Custom Search
Showing posts with label Linux basic commands. Show all posts
Showing posts with label Linux basic commands. Show all posts

How to power off Ubuntu server with Linux shutdown command

The Linux shutdown command has several options that you can use to bring down the Linux system. In the previous post, we've seen how to reboot Ubuntu server with shutdown command. In this post,  we are going to use the Linux shutdown command again. This time is to power off the Ubuntu server.

There are several options available with shutdown command that can be used to power off Linux system. One example is -h option, which means to halt the system after it has been brought down. We can add other things such as a comment message when invoking shutdown -h command. As you can see from the example of Ubuntu restart post before, we have to specify a time argument after the shutdown option so the system know when to shutdown the system. Here are some examples on how to power off Ubuntu server using shutdown command with other things that you can do:

1) Using Linux shutdown command with -h option example:

luzar@ubuntu:~$ sudo shutdown -h now
[sudo] password for luzar:

Broadcast message from luzar@ubuntu
        (/dev/pts/0) at 11:32 ...

The system is going down for halt NOW!

The example above show a basic shutdown command used to power off Ubuntu server. We used '-h' option and a time argument 'now' which means to bring down the system after we press enter.

 2) Using Linux shutdown command -p option example:

luzar@ubuntu:~$ sudo shutdown -h -P +1

Broadcast message from luzar@ubuntu
        (/dev/pts/0) at 11:47 ...

The system is going down for power off IN ONE MINUTE! 

The example above shows the '-P' option is added after the '-h' option. The -P option means 'power off'. When -P is used after -h option, it means we tell the system to power off after the system halt. Also this time we used +1 in the time argument. That means we want to bring down the system after 1 minute.

3) If you have to warn all your users, you can write a message after the time argument. See the example below:

luzar@ubuntu:~$ sudo shutdown -h -P +3 Please save your work now!

Broadcast message from luzar@ubuntu
        (/dev/pts/0) at 11:57 ...

The system is going down for power off in 3 minutes!
Please save your work now!
shutdown: Shutdown cancelled

If you want to cancel shutdown after invoked the command, you can press Ctrl+C combination keys. That will work if you do not use the 'now' time argument. If you just want to warn users to log out and prevent other user to login without actually bring the system down, you can use '-k' option.

luzar@ubuntu:~$ sudo shutdown -k +3 Please save your work now!

Broadcast message from luzar@ubuntu
        (/dev/pts/0) at 12:11 ...

The system is going down for maintenance in 3 minutes!
Please save your work now!
luzar@ubuntu:~$

That's all.

Ubuntu search files using find command basic

Basic find command




There are several ways to search files in Ubuntu. Probably you may already come across my other post about searching file in Ubuntu using locate command. Here is another way of searching files in Ubuntu. This time we are using the GNU find command. The find command comes with many options. That makes find a very powerful command. However, new users may not be very comfortable with find. I mean most users prefer a simple command, a command with less options to remember.




Don't ever think like that if you want to be a true Linux user. You must take advantage of powerful Linux command otherwise you would never see Linux full potential.




The manual page is a great help. Always make yourself a habit to read manual page for every new command. You don't have to read it all the first time you open it. My tip is, read the synopsis so you know the command's syntax. Then scan through some options. Some manual do have examples. Here are some informations about find in manual page:





NAME
find - search for files in a directory hierarchy


SYNOPSIS
find [-H] [-L] [-P] [path...] [expression]


DESCRIPTION
This manual page documents the GNU version of find. GNU find searches
the directory tree rooted at each given file name by evaluating the
given expression from left to right, according to the rules of precedence
(see section OPERATORS), until the outcome is known (the left
hand side is false for and operations, true for or), at which point
find moves on to the next file name.


If you are using find in an environment where security is important
(for example if you are using it to search directories that are writable
by other users), you should read the "Security Considerations" chapter
of the findutils documentation, which is called Finding Files and comes
with findutils. That document also includes a lot more detail and
discussion than this manual page, so you may find it a more useful
source of information.


EXPRESSIONS
The expression is made up of options (which affect overall operation
rather than the processing of a specific file, and always return true),
tests (which return a true or false value), and actions (which have side
effects and return a true or false value), all separated by operators.
-and is assumed where the operator is omitted.
If the expression contains no actions other than -prune, -print
is performed on all files for which the expression is true.



The meaning of synopsis options:





  • -H means do not follow symbolic links, except while processing the command line arguments.

  • -L means follow symbolic links.

  • -P means never follow symbolic links.




Let's try some examples to search file in Ubuntu using find command.




Example 1:




luzar@ubuntu:~$ find -P /home -iname File.txt
/home/luzar/File.txt
luzar@ubuntu:~$



In the example above, I used find command to search in /home directory for a file named file.txt. See that I used -iname instead of -name because -iname is case insensitive.




This concludes the basic find command guide. We'll continue advanced find command in another post.

Ubuntu help for beginners

Ubuntu help for beginners tutorial is a guide on how to find help in Ubuntu. I am not going for a long lecture because I know you're in trouble. Otherwise you won't looking for help, won't you? Don't worry, Ubuntu comes with several powerful online help to assist you working in Ubuntu. You don't need to call help desk. Here are what you need:

Using --help

The first thing you should try is --help. Most command have this option. The syntax is command --help. It'll display information about the command. Here is an example of --help:
luzar@ubuntu:~$ ls --help
Usage: ls [OPTION]... [FILE]...
List information about the FILEs (the current directory by default).
Sort entries alphabetically if none of -cftuvSUX nor --sort.

Mandatory arguments to long options are mandatory for short options too.
-a, --all do not ignore entries starting with .
-A, --almost-all do not list implied . and ..
--author with -l, print the author of each file
As you can see from the example above, --help is used with Ubuntu command to display help about the command's usage and options available.

Ubuntu help using man command

The man command is used to read the online reference manuals. Perhaps the man command is the most famous help in Ubuntu and other Unix-based system. It's a bit weird, but to open man manual, type man man. Here is a part of manual page about man command:
NAME
man - an interface to the on-line reference manuals

SYNOPSIS
man [-c|-w|-tZ] [-H[browser]] [-T[device]] [-X[dpi]] [-adhu7V] [-i|-I]
[-m system[,...]] [-L locale] [-p string] [-C file] [-M path] [-P
pager] [-r prompt] [-S list] [-e extension] [--warnings [warnings]]
[[section] page ...] ...
man -l [-7] [-tZ] [-H[browser]] [-T[device]] [-X[dpi]] [-p string] [-P
pager] [-r prompt] [--warnings[warnings]] file ...
man -k [apropos options] regexp ...
man -f [whatis options] page ...

DESCRIPTION
man is the systems manual pager. Each page argument given to man is
normally the name of a program, utility or function. The manual page
associated with each of these arguments is then found and displayed. A
section, if provided, will direct man to look only in that section of
the manual. The default action is to search in all of the available
sections, following a pre-defined order and to show only the first page
found, even if page exists in several sections.
Ubuntu (and all Linux) administration command manual is in section 8. You can see a section number is printed at the top of the page every time you open the manual page. See an example below:



Here are the complete section numbers of the manual and the
types of pages they contain.
1   Executable programs or shell commands
2 System calls (functions provided by the kernel)
3 Library calls (functions within program libraries)
4 Special files (usually found in /dev)
5 File formats and conventions eg /etc/passwd
6 Games
7 Miscellaneous (including macro packages and conven-
tions), e.g. man(7), groff(7)
8 System administration commands (usually only for root)
9 Kernel routines [Non standard]
Normally each manual page provides the information subject below:

  • NAME

  • SYNOPSIS

  • DESCRIPTION

  • OPTIONS

  • FILES

  • SEE ALSO

  • BUGS

  • AUTHOR

Ubuntu help using whatis command

Another Ubuntu help available is whatis command. The different between man command and whatis command is, whatis displays only the description. Here is a part of whatis manual page:
NAME
whatis - display manual page descriptions

SYNOPSIS
whatis [-dlhV] [-r|-w] [-s section] [-m system[,...]] [-M path] [-L
locale] [-C file] name ...

DESCRIPTION
Each manual page has a short description available within it. whatis
searches the manual page names and displays the manual page descripâ
tions of any name matched.

name may contain wildcards (-w) or be a regular expression (-r). Using
these options, it may be necessary to quote the name or escape (\) the
special characters to stop the shell from interpreting them.
Example of Ubuntu help using whatis command:
luzar@ubuntu:~$ whatis info
info (1) - read Info documents
info (5) - readable online documentation
luzar@ubuntu:~$

Ubuntu help using apropos command

Apropos command is another Ubuntu help for beginners. The apropos command search the manual page names and descriptions.
NAME
apropos - search the manual page names and descriptions

SYNOPSIS
apropos [-dalhV] [-e|-w|-r] [-s section] [-m system[,...]] [-M path]
[-L locale] [-C file] keyword ...

DESCRIPTION
Each manual page has a short description available within it. apropos
searches the descriptions for instances of keyword.
Example using apropos to get help in Ubuntu system:
luzar@ubuntu:~$ apropos adduser
/etc/adduser.conf (5) [adduser.conf] - configuration file for adduser(8) and ...
adduser (8) - add a user or group to the system
adduser.conf (5) - configuration file for adduser(8) and addgroup(8) .
luzar@ubuntu:~$

Ubuntu help using info command

Another help you can get in Ubuntu is info command. Info command prints info documentations. You can use info command to read a complete information about certain topic in Ubuntu. The info command is like a book. Here is an information in manual page:
NAME
info - read Info documents

SYNOPSIS
info [OPTION]... [MENU-ITEM...]

DESCRIPTION
Read documentation in Info format.
Example of info command:

luzar@ubuntu:~$ info cpio




As you see from the example above, info documentation is like a book. To read info documentation, you can use space bar to view next page. The up/down keys also can be used to scroll up/down the page.

Ubuntu useradd command

Ubuntu useradd command is a traditional Linux command to create a new user in the system. It is a low level utility for adding user or change new user default information. If you are new in Ubuntu, use the upper level utility, the adduser command instead.

Here are basic useradd command information:
  • Name: useradd - create a new user or update default new user information.
  • Synopsis:
useradd [options] LOGIN

useradd -D

useradd -D [options]

When invoked without the -D option, the useradd command creates a new user account using the values specified on the command line plus the default values from the system.

Ubuntu useradd command complete options:

-b, --base-dir BASE_DIR base directory for the new user account
home directory
-c, --comment COMMENT set the GECOS field for the new user account
-d, --home-dir HOME_DIR home directory for the new user account
-D, --defaults print or save modified default useradd
configuration
-e, --expiredate EXPIRE_DATE set account expiration date to EXPIRE_DATE
-f, --inactive INACTIVE set password inactive after expiration
to INACTIVE
-g, --gid GROUP force use GROUP for the new user account
-G, --groups GROUPS list of supplementary groups for the new
user account
-h, --help display this help message and exit
-k, --skel SKEL_DIR specify an alternative skel directory
-K, --key KEY=VALUE overrides /etc/login.defs defaults
-m, --create-home create home directory for the new user
account
-o, --non-unique allow create user with duplicate
(non-unique) UID
-p, --password PASSWORD use encrypted password for the new user
account
-r, --system create a system account
-s, --shell SHELL the login shell for the new user account
-u, --uid UID force use the UID for the new user account


Here are some examples on how to use useradd options when creating a new user account:

Example 1 - Ubuntu useradd command for creating a normal user account.

luzar@ubuntu:~$ sudo useradd -c 'Normal user' -d /home/jimi -m jimi
luzar@ubuntu:~$ sudo passwd jimi
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully
luzar@ubuntu:~$ ls -l /home/

total 12
drwxr-xr-x 2 aura aura 4096 2008-10-23 22:36 aura
drwxr-xr-x 2 jimi jimi 4096 2008-10-23 23:48 jimi
drwxr-xr-x 7 luzar luzar 4096 2008-10-23 05:43 luzar
luzar@ubuntu:~$

I used -c option to add comment to the user account. The -d is a home directory name and -m is to create home directory for the new user account. Finally, create a password for the new account to enabled it.

Example 2 - Ubuntu useradd command for creating user account for a temporary stuff.

luzar@ubuntu:~$ sudo useradd -c 'Temporary stuff' -e 2009-01-01 -f 7 -d /home/alex -m alex
luzar@ubuntu:~$ sudo passwd alex
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully

In the example above, I add -e option to change default values for expired date and -f for the number of days the account will be disabled after the password has expired. Then, as usual, don't forget to create a password for the new account.

Ubuntu add user command line

After working some times with Ubuntu, maybe it's time you need to create a new user account in your system. There are two common ways of creating user account in Ubuntu. One is using the traditional Linux useradd command. The other one is adduser command.

The different between useradd and adduser command:
  1. The useradd command is a lower level utility to create user. That means like other traditional Unix commands, it's powerful, has many options that you can use to customize user account such as home directory and groups.
  2. The adduser command is an upper level utility to create user. The command is suitable for new system administrator or new Ubuntu server user. It guides user step by step and automatically create user id(uid), group id(gid), create home directory, and copying files from `/etc/skel' to user home directory.
This is an example of Ubuntu add user command line:

luzar@ubuntu:~$ adduser aura
adduser: Only root may add a user or group to the system.
luzar@ubuntu:~$ sudo adduser aura
[sudo] password for luzar:
Adding user `aura' ...
Adding new group `aura' (1001) ...
Adding new user `aura' (1001) with group `aura' ...
Creating home directory `/home/aura' ...
Copying files from `/etc/skel' ...
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully
Changing the user information for aura
Enter the new value, or press ENTER for the default
Full Name []: auravisya
Room Number []:
Work Phone []:
Home Phone []:
Other []:
Is the information correct? [y/N] y
luzar@ubuntu:~$

That's very easy, isn't it?

Ubuntu create file in command line terminal

There are many ways of creating file in Ubuntu terminal. Everyone has his/her own way of creating file in terminal. I have two favorite ways of creating file. Way number one is the easy way, which I often use to create an empty file. Way number two is also an easy way which I always use when creating an important or large file.

The first way to create a file in Ubuntu terminal is by using touch command. Here are basic information about touch:
  • Name: touch - change file timestamps
  • Synopsis: touch [OPTION]... FILE...
  • Description:
Update the access and modification times of each FILE to the current time.
A FILE argument that does not exist is created empty.
A FILE argument string of - is handled specially and causes touch to change the times of the file associated with standard output.

The touch command actually used for changing file timestamps. But look at the italic sentence in the description above. That's how we are going to create file in Ubuntu terminal, the easy way I often use to create an empty file.

Example 1 - Ubuntu create file in terminal using touch command.

luzar@ubuntu:~/mycats$ ls
luzar@ubuntu:~/mycats$ touch ming
luzar@ubuntu:~/mycats$ ls -l
total 0
-rw-r--r-- 1 luzar luzar 0 2008-10-23 05:02 ming
luzar@ubuntu:~/mycats$

That's it. To create an empty file, just type touch filename. What I did in the example above is, I created an empty file named ming in the current directory.

Example 2 - Ubuntu create file in terminal, using touch command to create file in other directory.

luzar@ubuntu:~/mycats$ ls /home/luzar
file.txt folder mycats myfolder myjeans new_folder
luzar@ubuntu:~/mycats$ touch /home/luzar/myjeans/suarkuyak
luzar@ubuntu:~/mycats$ ls -l /home/luzar/myjeans/
total 0
-rw-r--r-- 1 luzar luzar 0 2008-10-23 05:11 suarkuyak
luzar@ubuntu:~/mycats$

If you want to create a file in other directory, use the same way as example 1, just add the correct path to the intended directory.

The second easy way that I use to create important or large file in Ubuntu terminal is using cp command. That's right. There's no way I'm going to type a large and important file. The cp is a command to copy files and directories.

  • Name: cp - copy files and directories
  • Synopsis:
cp [OPTION]... [-T] SOURCE DEST
cp [OPTION]... SOURCE... DIRECTORY
cp [OPTION]... -t DIRECTORY SOURCE...

Example 1 - Ubuntu create file in terminal, using cp command to create important file.

luzar@ubuntu:~$ cp /etc/network/interfaces /home/luzar/interfaces.bac
luzar@ubuntu:~$ ls -l ~
total 24
-rw-r--r-- 1 luzar luzar 0 2008-10-21 22:16 file.txt
drwxr-xr-x 3 luzar luzar 4096 2008-10-23 05:00 folder
-rw-r--r-- 1 luzar luzar 268 2008-10-23 05:43 interfaces.bac
drwxr-xr-x 2 luzar luzar 4096 2008-10-23 05:02 mycats
drwxr-xr-x 3 luzar luzar 4096 2008-10-22 05:02 myfolder
drwxr-xr-x 2 luzar luzar 4096 2008-10-23 05:11 myjeans
drwxr-xr-x 2 luzar luzar 4096 2008-10-22 03:43 new_folder
luzar@ubuntu:~$

Do you know why I use cp command to create an important file? The reason is simple. An important file normally is an existing file. How can an empty file be important? Always remember this, creating file is very easy in Ubuntu terminal. You don't have to type everything. So, if you are working in terminal, you probably configuring something or editing something. That always involve Ubuntu important file. So create the same file you are editing with the same way I did above. Make a copy of the file before you start messing with it. In case something wrong happened, you can always start over.

If you really need to type a new content to a file in Ubuntu terminal, then use a text editor. There are two powerful text editors in Ubuntu, VI editor and Emacs. You can check my post Ubuntu command line text editor for Vi text editor basic tutorial.

Ubuntu create directory

The command to create directory in Ubuntu is mkdir. Here are the mkdir command information and synopsis.

mkdir - make directories.

Synopsis:
mkdir [OPTION] DIRECTORY...

Example of mkdir command used with some important options:

Example 1 - Ubuntu create directory using mkdir with no option.
luzar@ubuntu:~$ ls
file.txt
luzar@ubuntu:~$ mkdir folder
luzar@ubuntu:~$ ls
file.txt folder
luzar@ubuntu:~$


The mkdir command with no option creates a directory.

Example 2 - Ubuntu create multiple directories using mkdir with no option.
luzar@ubuntu:~$ mkdir new_folder myfolder mycats myjeans
luzar@ubuntu:~$ ls
file.txt folder mycats myfolder myjeans new_folder
luzar@ubuntu:~$


Use mkdir to creates multiple directory at once. Use mkdir command with no option and multiple directory's names separated by a space.

Example 3 - Ubuntu create directory in other directory using mkdir.
luzar@ubuntu:~$ mkdir folder/mydir
luzar@ubuntu:~$ ls -R
.:
file.txt folder mycats myfolder myjeans new_folder

./folder:
mydir

./folder/mydir:

./mycats:

./myfolder:

./myjeans:

./new_folder:
luzar@ubuntu:~$


To create a directory in another directory, you don't have to change into that directory. Just provide the path like in the example above.

Example 4 - Ubuntu create directory in other directory using mkdir - m option.
luzar@ubuntu:~$ ls
file.txt folder mycats myfolder myjeans new_folder
luzar@ubuntu:~$ mkdir -m 0022 myfolder/umaskdir
luzar@ubuntu:~$ ls -l myfolder/umaskdir/
ls: cannot open directory myfolder/umaskdir/: Permission denied
luzar@ubuntu:~$ ls -l myfolder/
total 4
d----w--w- 2 luzar luzar 4096 2008-10-22 05:02 umaskdir
luzar@ubuntu:~$


In this example, I created a new directory named umaskdir in myfolder directory. This time I added -m option to mkdir and a umask permission to apply to the new directory. Then, I tried to view umaskdir content with ls -l myfolder/umaskdir command. I got a permission denied warning. That means I don't have access permission to the directory I created myself. So I viewed myfolder directory content to see details about umaskdir directory permission. That's what -m option do, apply permission to the new directory using umask.

Ubuntu delete file

The rm command is a very powerful command. We learned how to delete an empty folder in my previous post, the rmdir command guide. It'd take some times if you are going to delete a folder with lots of contents. But don't worry. The rm command can solve that problem.

Here are basic information about rm command:

rm - remove files or directories

rm format:
rm [OPTION]... FILE...

Example of rm command used with some important options:

Example 1 - Ubuntu delete file using rm with no option.

luzar@ubuntu:~$
ls -l
total 4
-rw-r--r-- 1 luzar luzar 0 2008-10-21 21:36 file.txt
drwxr-xr-x 3 luzar luzar 4096 2008-10-21 21:37
folder
luzar@ubuntu:~$
rm file.txt
luzar@ubuntu:~$
ls -l
total 4
drwxr-xr-x 3 luzar luzar 4096 2008-10-21 21:37
folder
luzar@ubuntu:~$

The rm command with no option deletes a file specified.

Example 2 - Ubuntu delete file using rm with -r option.

luzar@ubuntu:~$
ls -l
total 4
drwxr-xr-x 3 luzar luzar 4096 2008-10-21 21:37
folder
luzar@ubuntu:~$
rm -r folder/
luzar@ubuntu:~$
ls -l
total 0
luzar@ubuntu:~$

The rm command by default cannot delete a directory. Use -r or -R option to remove directories and their contents recursively.

Example 3 - Ubuntu delete file using rm with -i option.

luzar@ubuntu:~$
ls -l
total 4
-rw-r--r-- 1 luzar luzar 0 2008-10-21 22:16 file.txt
drwxr-xr-x 3 luzar luzar 4096 2008-10-21 22:16
folder
luzar@ubuntu:~$
rm -i folder/
rm: cannot remove `folder/': Is a directory
luzar@ubuntu:~$
rm -ri folder/
rm: descend into directory `folder/'? n
luzar@ubuntu:~$

Here are some interesting examples. First, I list the directory content. Then I proved that rm cannot delete a directory unless -r or -R is given. Finally, I used rm -ri and we can see that -i option prompts for confirmation before deleting a file/directory. You can answer with y for yes or n for no.

Example 4 - Ubuntu delete file using rm with -f option.

luzar@ubuntu:~$
ls -l
total 4
-rw-r--r-- 1 luzar luzar 0 2008-10-21 22:16 file.txt
drwxr-xr-x 3 luzar luzar 4096 2008-10-21 22:16
folder
luzar@ubuntu:~$
rm -f folder
rm: cannot remove `folder': Is a directory
luzar@ubuntu:~$
rm -rf folder/
luzar@ubuntu:~$
ls -l
total 0
-rw-r--r-- 1 luzar luzar 0 2008-10-21 22:16 file.txt
luzar@ubuntu:~$

The -f option does not prompt for confirmation. It is used to force delete. This option is very useful when deleting a directory full of contents. Just don't use rm -rf / with root privilege because that will completely remove all Ubuntu file systems.

Example 5 - Ubuntu delete file using rm with -v option.

luzar@ubuntu:~$
ls -R
.:
file.txt
folder

./folder:
new_files.txt
new_folder

./folder/new_folder:
luzar@ubuntu:~$
rm -rv folder/
removed directory: `folder/new_folder'
removed `folder/new_files.txt'
removed directory: `folder'
luzar@ubuntu:~$

If you don't want to be prompt but need to see what files or directories you deleted, then use -v option. From the example above, I list the directory content with ls -R to show subdirectory recursively. Then I used rm -rv to delete all contents in the folder verbosely.

Ubuntu ls command

The ls command is perhaps the mostly used command if you are working in the Ubuntu terminal. It is used to list directory contents. In other words, the ls command is used to view contents in a directory. There are many options available in ls command. The ls command options help users to list or view the way they prefer.

If you read my previous Ubuntu permission post, then you should have noticed one of the ls command options that I used, which is the
ls -l option. Actually the -l is an option to view long listing format. That way we can see the details of the file, such as the permission and file ownership. Below are examples of other useful ls command options normally used in Ubuntu.

Example 1 - Ubuntu standalone ls command, no option.



ls command is one of Ubuntu commands that can be used without an option. As you can see in the picture above, the ls command without option will list the directory contents. A directory, file and link is identified with different colors.

Example 2 - Ubuntu ls command with -a option.

luzar@ubuntu:/$ ls -a
.    boot   etc     initrd.img  media  proc  sbin  tmp  vmlinuz
.. cdrom home lib mnt .rnd srv usr
bin dev initrd lost+found opt root sys var

luzar@ubuntu:/$


The -a option lists all contents in directory including hidden files. A few years ago, people hide their nasty file using . and whitespace. But today, that trick won't work anymore.

Example 3 - Ubuntu ls command with -F option.

luzar@ubuntu:~$ ls -F
file.txt* folder/
luzar@ubuntu:~$


The -F option prints file types.

Example 4 - Ubuntu ls command with -g option.

luzar@ubuntu:~$ ls -g
total 4
-rwxrwxrwx 1 luzar 0 2008-10-21 03:43 file.txt
drwx------ 3 luzar 4096 2008-10-21 11:14 folder
luzar@ubuntu:~$


The -g option prints long listing like -l but without owner. To prints owner without group, use -o.

Example 5 - Ubuntu ls command with -h option.

luzar@ubuntu:~$ ls -h
file.txt folder
luzar@ubuntu:~$ ls -hl
total 4.0K
-rwxrwxrwx 1 luzar luzar 0 2008-10-21 03:43 file.txt
drwx------ 3 luzar luzar 4.0K 2008-10-21 11:14 folder
luzar@ubuntu:~$


The -h option itself means nothing. But combine with -l, it prints size in human readable format.

Example 6 - Ubuntu ls command with -i option.

luzar@ubuntu:~$ ls -i
8201 file.txt 8200 folder
luzar@ubuntu:~$


The -i option prints the index number of each file.

Example 7 - Ubuntu ls command with -m option.

luzar@ubuntu:~$ ls -m /var
backups, cache, lib, local, lock, log, mail, opt, run, spool, tmp, www
luzar@ubuntu:~$


The -m option prints a comma separated list of entries.

Example 8 - Ubuntu ls command with -r option.

luzar@ubuntu:~$ ls -r /var
www tmp spool run opt mail log lock local lib cache backups
luzar@ubuntu:~$ ls -rl /var
total 40
drwxr-xr-x 2 root root 4096 2008-10-19 15:17 www
drwxrwxrwt 2 root root 4096 2008-06-13 10:14 tmp
drwxr-xr-x 4 root root 4096 2008-10-19 15:17 spool
drwxr-xr-x 11 root root 400 2008-10-21 09:22 run
drwxr-xr-x 2 root root 4096 2008-10-19 15:10 opt
drwxrwsr-x 2 root mail 4096 2008-10-20 13:53 mail
drwxr-xr-x 10 root root 4096 2008-10-21 09:22 log
drwxrwxrwt 3 root root 60 2008-10-21 09:22 lock
drwxrwsr-x 2 root staff 4096 2008-06-13 10:14 local
drwxr-xr-x 27 root root 4096 2008-10-19 15:19 lib
drwxr-xr-x 9 root root 4096 2008-10-19 15:17 cache
drwxr-xr-x 2 root root 4096 2008-10-19 15:10 backups

luzar@ubuntu:~$


The -r option prints directory content reversely.

Example 9 - Ubuntu ls command with -R option.

luzar@ubuntu:~$ ls -R
.:
file.txt folder

./folder:
new_file.txt new_folder

./folder/new_folder:
luzar@ubuntu:~$


The -R option lists subdirectory recursively. As from the example, we can also see the content in the folder directory.

Example 10 - Ubuntu ls command with -s option.

luzar@ubuntu:~$ ls -s
total 4
0 file.txt 4 folder
luzar@ubuntu:~$


The -s option sorts content by file size.

That's ten examples of useful Ubuntu ls command option guides that you can use to manage your system. Another useful options are:

-t = to sort content by modification time.
-X = to sort alphabetically by extention.
-v = to sort by version.

Ubuntu rmdir

The rmdir is a command to remove an empty directory in Ubuntu. Here is an example on how to run Ubuntu rmdir from the terminal:


luzar@ubuntu:~/linux$ ls -l
total 4
drwxr-xr-x 2 luzar luzar 4096 2008-10-20 03:58 tutorials
luzar@ubuntu:~/linux$ rmdir tutorials/
rmdir: failed to remove `tutorials/': Directory not empty
luzar@ubuntu:~/linux$


See the warning, rmdir: failed to remove `tutorials/': Directory not empty. The rmdir cannot remove the directory because it's not empty. So we need to remove all contents in the directory before we can use the Ubuntu rmdir command again. Use ls command with -l option to list the directory content:


luzar@ubuntu:~/linux$ ls -l tutorials/
total 0
-rw-r--r-- 1 luzar luzar 0 2008-10-20 03:58 ubuntu
luzar@ubuntu:~/linux$


So there's only one file in the directory. How do I know that it is a file? I'll tell you later because now we need to remove the file. Use rm command to remove file in Ubuntu. It doesn't matter if it's empty or not.


luzar@ubuntu:~/linux$ rm tutorials/ubuntu
luzar@ubuntu:~/linux$ ls -l tutorials/
total 0
luzar@ubuntu:~/linux$


The directory is empty now. Let's try to delete the directory again.


luzar@ubuntu:~/linux$ cd ..
luzar@ubuntu:~$ pwd
/home/luzar
luzar@ubuntu:~$ ls
linux
luzar@ubuntu:~$ rmdir -p linux/tutorials/
luzar@ubuntu:~$ ls -l
total 0
luzar@ubuntu:~$


I use the cd .. command to move to upper directory. Then I use pwd command to check my current working directory. See that this time I used rmdir with -p option. The -p option means please remove the DIRECTORY and its ancestors. That's why I move up one level earlier.

That's all about Ubuntu rmdir. Or that's the only thing I know about rmdir, to be more accurate.