Basic Linux Commands – Part 5

By | March 31, 2018

In this blog, we will be discussing the Linux files related commands. Most of the times we will be dealing with the files and directories in Linux which includes, creating, deleting, copying, renaming and many more operations on files and directories. In this blog, we will cover the below commands.

  • ls
  • cd
  • mkdir
  • cp
  • mv
  • rm
  • rmdir
  • cat
  • which
  • tree

1.  ls

ls command list out the files and directories in the current working directory, in short, it lists out the directory contents. It has several options.

[root@devopsage ~]# ls
sample.txt file1.txt file2.txt
[root@devopsage ~]#

Options:

ls – l: long listing of the files with its various attributes.

[root@devopsage ~]# ls -l
total 2097152
-rw-r--r--. 1 root root          0 Mar 31 05:36 file1.txt
-rw-r--r--. 1 root root          0 Mar 31 05:36 file2.txt
-rw-r--r--. 1 root root          0 Mar 31 05:32 sample.txt
-rw-r--r--. 1 root root 2147483648 Jan  7  2015 swap
[root@devopsage ~]#

Other Sample Examples:

  • ls -l filename: to see the various attributes of a particular file.
  • ls -al: list out al the files in long listing format along with the hidden files. “-a” is to show the hidden files
  • ls -ld: list out directories only.
  • ls -lr: reverse listing of files and directories
  • ls -lrt: reverse listing of files with the timestamp
  • ls -ld a*: list out all the directories starting with the letter “a”. here “*” means wildcard.
  • ls -ld directoryname: list out particular directory.
  • ls [am]*: It means the first character of the file must be “a” or “e”.
  • ls [!am]*: Symbol “!” means files starting with letter  “a” or “m” will not be listed.
  • ls [a-m][b-h][1-8]: list out the files in the specific range defined.

2. cd

cd stands for change directory, it os one of the most widely used command by any user in Unix/Linux operating system. This command is used to change the present working directory.

Note: If this command is used without an argument, the user will be taken back to its home directory regardless of the location he is currently working on.

Options:

  • cd .   : means current directory.
  • cd ..  : will take you to the parent directory
  • cd ~  : This will take you to the home directory of the user.
  • cd –   : Will take you to the previous directory, it’s very useful you want to switch between to 2               different directories.

3. mkdir

This command is used to make a directory accepting the name of the directory as an argument.

[root@devopsage ~]# mkdir devopsage
[root@devopsage ~]# ls -lrt
total 2097152
-rw-r--r--. 1 root root 2147483648 Jan  7  2015 swap
-rw-r--r--. 1 root root          0 Mar 31 05:32 sample.txt
-rw-r--r--. 1 root root          0 Mar 31 05:36 file2.txt
-rw-r--r--. 1 root root          0 Mar 31 05:36 file1.txt
-rw-r--r--. 1 root root          0 Mar 31 05:48 abc.txt
drwxr-xr-x. 2 root root          6 Mar 31 06:07 devopsage
[root@devopsage ~]#

If we specify “-p” options, then it will create a collaborative directory i.e, it creates a directory and all its parent directories.

[root@devopsage ~]# mkdir unix/linux/aix/solaris   // fails because there is no existing directory "unix"
mkdir: cannot create directory ‘unix/linux/aix/solaris’: No such file or directory

[root@devopsage ~]# mkdir -p unix/linux/aix/solaris
[root@devopsage ~]# cd unix/
[root@devopsage unix]# ls
linux
[root@devopsage unix]# cd linux/
[root@devopsage linux]# ls
aix
[root@devopsage linux]# cd aix/
[root@devopsage aix]# ls
solaris
[root@devopsage aix]# cd solaris/
[root@devopsage solaris]# ls
[root@devopsage solaris]#

We can also create multiple directories inside a directory in a hierarchical fashion. refer the below example.

Execute the below command to get the directory structure shown in the figure above,

[root@devopsage ~]# mkdir -p Cloud/{OS/{Linux,Windows},Databases/{MySQL,Oracle},DevOps/{jenkins,Docker}}

[root@devopsage ~]# ls -lrt
total 2097152
-rw-r--r--. 1 root root 2147483648 Jan  7  2015 swap
-rw-r--r--. 1 root root          0 Mar 31 05:32 sample.txt
-rw-r--r--. 1 root root          0 Mar 31 05:36 file2.txt
-rw-r--r--. 1 root root          0 Mar 31 05:36 file1.txt
-rw-r--r--. 1 root root          0 Mar 31 05:48 abc.txt
drwxr-xr-x. 2 root root          6 Mar 31 06:07 devopsage
drwxr-xr-x. 3 root root         18 Mar 31 06:10 unix
drwxr-xr-x. 5 root root         44 Mar 31 06:29 Cloud

[root@devopsage ~]# tree Cloud
Cloud
├── Databases
│   ├── MySQL
│   └── Oracle
├── DevOps
│   ├── Docker
│   └── jenkins
└── OS
    ├── Linux
    └── Windows

9 directories, 0 files
[root@devopsage ~]#

4. tree

This command list contents of directories in a tree-like format. the example of it we have seen above.

Note: You may have to install this command first

  • Centos/RedHat: # yum install tree
  • Ubuntu/Debian: # apt-get install tree
[root@devopsage ~]# tree /root
/root
├── abc.txt
├── Cloud
│   ├── Databases
│   │   ├── MySQL
│   │   └── Oracle
│   ├── DevOps
│   │   ├── Docker
│   │   └── jenkins
│   └── OS
│       ├── Linux
│       └── Windows
├── devopsage
├── file1.txt
├── file2.txt
├── sample.txt
├── swap
└── unix
    └── linux
        └── aix
            └── solaris

15 directories, 5 files
[root@devopsage ~]#

5. cp

cp command is used to copy a file and a directory from one location to other. We can do below things using “cp” command.

  • make a copy of a file with another name
  • copy file from one place to another, i.e from one directory to another
  • copy multiple files from one location to another
  • copy directory from one location to another

Let’s see this in details.

Copying one file to another, i.e creating an exact copy of the existing file.

[root@devopsage ~]# cat test.txt 
devopsage technology
[root@devopsage ~]# cp test.txt anothe-test.txt
[root@devopsage ~]# cat anothe-test.txt 
devopsage technology
[root@devopsage ~]# ls -lrt
total 2097160
-rw-r--r--. 1 root root 2147483648 Jan  7  2015 swap
-rw-r--r--. 1 root root          0 Mar 31 05:32 sample.txt
-rw-r--r--. 1 root root          0 Mar 31 05:36 file2.txt
-rw-r--r--. 1 root root          0 Mar 31 05:36 file1.txt
-rw-r--r--. 1 root root          0 Mar 31 05:48 abc.txt
drwxr-xr-x. 2 root root          6 Mar 31 06:07 devopsage
drwxr-xr-x. 3 root root         18 Mar 31 06:10 unix
drwxr-xr-x. 5 root root         44 Mar 31 06:29 Cloud
-rw-r--r--. 1 root root         21 Mar 31 06:45 test.txt
-rw-r--r--. 1 root root         21 Mar 31 06:45 anothe-test.txt
[root@devopsage ~]#

Copying file to different directories

[root@devopsage ~]# cp test.txt devopsage/
[root@devopsage ~]# ls -l devopsage/
total 4
-rw-r--r--. 1 root root 21 Mar 31 06:49 test.txt
[root@devopsage ~]#

Copying multiple files to a directory

[root@devopsage ~]# cp abc.txt anothe-test.txt file1.txt devopsage/
[root@devopsage ~]# ls -l devopsage/
total 8
-rw-r--r--. 1 root root  0 Mar 31 06:50 abc.txt
-rw-r--r--. 1 root root 21 Mar 31 06:50 anothe-test.txt
-rw-r--r--. 1 root root  0 Mar 31 06:50 file1.txt
-rw-r--r--. 1 root root 21 Mar 31 06:49 test.txt
[root@devopsage ~]#

We can also use wildcard (*) to copy multiple files to another directory.

[root@devopsage devops]# cp file* testdirectory
[root@devopsage devops]# ls -lrt testdirectory/
total 0
-rw-r--r--. 1 root root 0 Mar 31 06:53 file5
-rw-r--r--. 1 root root 0 Mar 31 06:53 file4
-rw-r--r--. 1 root root 0 Mar 31 06:53 file3
-rw-r--r--. 1 root root 0 Mar 31 06:53 file2
-rw-r--r--. 1 root root 0 Mar 31 06:53 file1
[root@devopsage devops]#

Copying a directory requires “-r” option, or else it will through an error!!

[root@devopsage ~]# cp -rv devops unix
[root@devopsage ~]# cd unix/
[root@devopsage unix]# ls -l
total 0
drwxr-xr-x. 3 root root 86 Mar 31 06:56 devops
drwxr-xr-x. 3 root root 16 Mar 31 06:10 linux
[root@devopsage unix]#

6. mv

mv command is used to move the file or a directory from one location to another (cut & paste). It is also used to rename files and directories.

Moving a file and directory. # mv <sourcefile> <destination location>

[root@devopsage unix]# ls -l
total 0
drwxr-xr-x. 3 root root 86 Mar 31 06:56 dev
-rw-r--r--. 1 root root  0 Mar 31 07:01 hello.sh
drwxr-xr-x. 3 root root 16 Mar 31 06:10 linux

[root@devopsage unix]# mv hello.sh linux/
[root@devopsage unix]# ls -l
total 0
drwxr-xr-x. 3 root root 86 Mar 31 06:56 dev
drwxr-xr-x. 3 root root 31 Mar 31 07:01 linux
[root@devopsage unix]#

Renaming a file or directory. # mv <old_file_name> <new_name>

[root@devopsage unix]# ls -l
total 0
drwxr-xr-x. 3 root root 86 Mar 31 06:56 dev
drwxr-xr-x. 3 root root 31 Mar 31 07:01 linux
[root@devopsage unix]# mv dev developer
[root@devopsage unix]# ls -l
total 0
drwxr-xr-x. 3 root root 86 Mar 31 06:56 developer
drwxr-xr-x. 3 root root 31 Mar 31 07:01 linux
[root@devopsage unix]#

Note: mv command works exactly same for both files and directories (In both the cases renaming and moving)

7. cat

cat (concatenate) command is used to display the content of any files. using cat command we can also modify the content of any file and often create a new file.

[root@devopsage ~]# cat > devopsage   //write something and press ctrl+d to same the file.
welcome to devopsage
ctrl+d to save.

[root@devopsage ~]# ls -l
-rw-r--r--. 1 root root         21 Feb 24 10:58 devopsage

[root@devopsage ~]# cat devopsage 
welcome to devopsage

Few Options:

–E: Displays $ at the end of the line.
-n: display the line number for the printed output

8. rm

rm command deletes one or more files or directories. It should be used very carefully in production servers because it does not return any output.

removing a file with prompt.

[root@devopsage unix]# ls -l
total 0
drwxr-xr-x. 3 root root 86 Mar 31 06:56 developer
-rw-r--r--. 1 root root  0 Mar 31 07:12 file1
-rw-r--r--. 1 root root  0 Mar 31 07:12 file2
-rw-r--r--. 1 root root  0 Mar 31 07:12 file3
drwxr-xr-x. 3 root root 31 Mar 31 07:01 linux

[root@devopsage unix]# rm -i file1 
rm: remove regular empty file ‘file1’? y

[root@devopsage unix]# ls -l
total 0
drwxr-xr-x. 3 root root 86 Mar 31 06:56 developer
-rw-r--r--. 1 root root  0 Mar 31 07:12 file2
-rw-r--r--. 1 root root  0 Mar 31 07:12 file3
drwxr-xr-x. 3 root root 31 Mar 31 07:01 linux
[root@devopsage unix]#

deleting a file without prompt using “-f” option (f: forcefully)

[root@devopsage unix]# ls -l
total 0
drwxr-xr-x. 3 root root 86 Mar 31 06:56 developer
-rw-r--r--. 1 root root  0 Mar 31 07:12 file2
-rw-r--r--. 1 root root  0 Mar 31 07:12 file3
drwxr-xr-x. 3 root root 31 Mar 31 07:01 linux

[root@devopsage unix]# rm -f file2
[root@devopsage unix]# ls -l

total 0
drwxr-xr-x. 3 root root 86 Mar 31 06:56 developer
-rw-r--r--. 1 root root  0 Mar 31 07:12 file3
drwxr-xr-x. 3 root root 31 Mar 31 07:01 linux
[root@devopsage unix]#

Removing a directory with files and directory inside it.

[root@devopsage unix]# ls -l
total 0
drwxr-xr-x. 3 root root 86 Mar 31 06:56 developer
-rw-r--r--. 1 root root  0 Mar 31 07:12 file3
drwxr-xr-x. 3 root root 31 Mar 31 07:01 linux

[root@devopsage unix]# rm -rf linux/
[root@devopsage unix]# ls -l
total 0
drwxr-xr-x. 3 root root 86 Mar 31 06:56 developer
-rw-r--r--. 1 root root  0 Mar 31 07:12 file3
[root@devopsage unix]#

Here,
-r: recursive
-f: forcefully

Deleting all files and directories at once.

[root@devopsage unix]# ls -lrt
total 0
drwxr-xr-x. 3 root root 86 Mar 31 06:56 developer
-rw-r--r--. 1 root root  0 Mar 31 07:12 file3
[root@devopsage unix]# rm -rf *
[root@devopsage unix]# ls -l
total 0
[root@devopsage unix]#

9. rmdir

This command will delete an empty directory. This command will return an error if the directory is not empty.

[root@devopsage devops]# ls -l
total 0
drwxr-xr-x. 2 root root 66 Mar 31 06:53 testdirectory

[root@devopsage devops]# rmdir testdirectory/
rmdir: failed to remove ‘testdirectory/’: Directory not empty

[root@devopsage devops]# rm -rf testdirectory/*
[root@devopsage devops]# ls -l
total 0
drwxr-xr-x. 2 root root 6 Mar 31 07:21 testdirectory
[root@devopsage devops]# rmdir testdirectory/
[root@devopsage devops]#

10. which

which command shows the full path of a command

[root@devopsage ~]# which ping 
/bin/ping
[root@devopsage ~]#

 

Leave a Reply

Your email address will not be published. Required fields are marked *