How to pass RHCE/RHCT exam! – part 3

Today we’re going to focus on how to use standard command line tools (e.g. ls, cp, mv, rm, tail, cat, etc.) to create, remove, view, and investigate files and directories.

If you are a beginner, the first thing I suggest is to check the current directory to avoid messing up things. This is simply done with pwd (print working directory):

[[email protected] ~]# pwd
/root
[[email protected] ~]#

Another basic command is ls. This just lists the files and directories inside the path given as an argument. If no path is specified, it will list all the files and directories from the current location:

[[email protected] ~/exam]# ls
directory1  file1
[[email protected] ~/exam]#
[[email protected] ~/exam]# ls /etc/httpd/
conf  conf.d  logs  modules  run
[[email protected] ~/exam]#

There are also hidden files and directories in GNU/Linux. These start with a dot “.” and can be seen by using the -a option for ls. To list the contents of a directory as a column, you may use the -l parameter. And to have the file sizes in a human readable form, just use -h.

[[email protected] ~/exam]# ls -a
.  ..  directory1  file1  .hidden
[[email protected] ~/exam]# ls -l
total 4
drwxr-xr-x 2 root root 4096 May  1 23:07 directory1
-rw-r--r-- 1 root root    0 May  1 23:06 file1
[[email protected] ~/exam]# ls -alh
total 20K
drwxr-xr-x  4 root root 4.0K May  1 23:12 .
drwxr-x--- 16 root root 4.0K May  1 23:06 ..
drwxr-xr-x  2 root root 4.0K May  1 23:07 directory1
-rw-r--r--  1 root root    0 May  1 23:06 file1
drwxr-xr-x  2 root root 4.0K May  1 23:12 .hidden
[[email protected] ~/exam]#

The basic tool to copy files or directories is named cp. To copy a whole directory you must use the -r option for recursive:

[[email protected] ~/exam]# ls
directory1  file1
[[email protected] ~/exam]#
[[email protected] ~/exam]# cp file1 file2
[[email protected] ~/exam]#
[[email protected] ~/exam]# ls
directory1  file1  file2
[[email protected] ~/exam]#

To copy a directory:

[[email protected] ~/exam]# ls
directory1  file1  file2
[[email protected] ~/exam]# cp -r directory1/ directory2
[[email protected] ~/exam]# ls
directory1  directory2  file1  file2
[[email protected] ~/exam]#

For more options, you may check the manual page for cp using

man cp

There is also a tool available for renaming/moving files or directories. It’s called mv and may be used like this:
-to rename a file

[[email protected] ~/exam]# ls
directory1  directory2  file1  file2
[[email protected] ~/exam]# mv file1 file1-renamed
[[email protected] ~/exam]# ls
directory1  directory2  file1-renamed  file2
[[email protected] ~/exam]#

-to rename a directory

[[email protected] ~/exam]# ls
directory1  directory2  file1-renamed  file2
[[email protected] ~/exam]# mv directory2/ directory2-renamed
[[email protected] ~/exam]# ls
directory1  directory2-renamed  file1-renamed  file2
[[email protected] ~/exam]#

-to move a file/directory to another location just specify the full/relative path

[[email protected] ~/exam]# mv directory2-renamed/ directory1/directory2/
[[email protected] ~/exam]# ls
directory1  file1-renamed  file2
[[email protected] ~/exam]# ls directory1/
directory2
[[email protected] ~/exam]#

Another basic but important tool is rm which is used to remove files or directories. Like in the case of cp, in order to remove a directory the -r (recursive) option must be specified:

[[email protected] ~/exam]# ls
directory1  file1-renamed  file2
[[email protected] ~/exam]# rm file2
rm: remove regular empty file `file2'? y
[[email protected] ~/exam]# rm -r directory1/directory2/
rm: remove directory `directory1/directory2/'? y
[[email protected] ~/exam]# ls
directory1  file1-renamed
[[email protected] ~/exam]# ls directory1/
[[email protected] ~/exam]#

To avoid being asked for confirmation use the -f (force) option, or use the full path for rm command, because rm is actually an alias to /bin/rm -i (interactive):

[[email protected] ~/exam]# which rm
alias rm='rm -i'
        /bin/rm
[[email protected] ~/exam]#

cat is the tool that will display the contents of a file for you. For example to see the contents of a file named example.txt you can use:

[[email protected] ~/exam]# cat example.txt

1
2
a - This is a letter
This is a test line
3 - And this is a number

[[email protected] ~/exam]# 

If you want to search for a certain string, you send the output from a given command to a tool named grep:

[[email protected] ~/exam]# cat example.txt |grep test

This is a test line
[[email protected] ~/exam]#

This will print out all the lines containing the string test. In our case there was just 1 line.