Basic Commands

Creating, Removing, Copying, Moving files & Directories

Creating a file in Linux using cat command:

  • cat (Concatenate) command is used to create a file and to display and modify the contents of a file.
  • To create a file

# cat > filename (say lbfile).
Linux Basics
Ctrl+d (To save the file).

[root@localhost ~]# cat > lbfile
Linux basics
[root@localhost ~]#

 

To display the content of the file
# cat filename (say lbfile)

[root@localhost ~]# cat lbfile
Linux Basics
[root@localhost ~]#

 

To append the data in the already existing file

# cat >> <filename>
# cat >> lbfile
Ctrl+d (to save the changes)

[root@localhost ~]# cat >> lbfile
RedHat
[root@localhost ~]# cat lbfile
Linux Basics
RedHat
[root@localhost ~]#

Creating multiple files at same time using touch command

#touch <filename> <filename> <filename>
#touch file1 file2 file3
Note: to check the files use # ls command

[root@localhost ~]# touch file1 file2 file3
[root@localhost ~]# ls[root@localhost ~]# ls anaconda-ks.cfg Downloads initial-setup-ks.cfg lbfile Public Desktop file1 Music Templates Videos Documents file3 file2 Pictures [root@localhost ~]#

Creating a Directory:
#mkdir <dir name>
#mkdir lbdir

[root@localhost ~]# mkdir lbdir
[root@localhost ~]# ls
anaconda-ks.cfg Downloads file3 lbfile Public
Desktop file1 initial-setup-ks.cfg Music Templates
Documents file2 lbdir Pictures Videos
[root@localhost ~]#

 

PWD(Print work directory)
PWD is display the current working directory.

[root@localhost Downloads]# pwd
/root/Downloads
[root@localhost Downloads]# pwd -P
/root/Downloads
[root@localhost Downloads]# pwd -L
/root/Downloads
[root@localhost Downloads]#

Copying files into directory
#cp <source filename> <destination directory in which to paste the file>
#cp file1 lbdir

[root@localhost ~]# cp file1 lbdir/
[root@localhost ~]# cd lbdir/
[root@localhost lbdir]# ls
file1
[root@localhost lbdir]#

Copying directories from one location to other
# cp –rvfp <dir name> <destination name>
#cp –rvfp lbdir1 lbdir

[root@localhost ~]# mkdir lbdir1
[root@localhost ~]# cp -rvfp lbdir1 lbdir
‘lbdir1’ -> ‘lbdir/lbdir1’
[root@localhost ~]# ls lbdir
file1 lbdir1
[root@localhost ~]#

Moving files from one location to other (cut and Paste)
#mv <filename> <Destination directory>
#mv file2 lbdir

[root@localhost ~]# mv file2 lbdir
[root@localhost ~]# ls
anaconda-ks.cfg Downloads file3 lbfile Public
Desktop file1 initial-setup-ks.cfg Music Templates
Documents lbdir Pictures Videos [root@localhost ~]# ls lbdir
file1 file2 lbdir1 [root@localhost ~]#

Renaming a File / Directory

  • The procedure and command for renaming a file is exactly same as renaming the directory.
    #mv <old name> <new name>
    #mv lbfile linuxfile
[root@localhost ~]# ls
anaconda-ks.cfg Downloads file3 lbfile Public
Desktop file1 initial-setup-ks.cfg Music Templates
Documents lbdir Pictures Videos
[root@localhost ~]# mv lbfile linuxfile
[root@localhost ~]# ls
anaconda-ks.cfg Downloads file3 linuxfile Public
Desktop file1 initial-setup-ks.cfg Music Templates
Documents lbdir Pictures Videos
[root@localhost ~]#

Removing a File
#rm filename or #rm –f filename (without prompting)

[root@localhost ~]# rm file1
rm: remove regular empty file ‘file1’? y

without prompting:-

[root@localhost ~]# rm -f file3
[root@localhost ~]# ls
anaconda-ks.cfg Downloads linuxfile Public Documents
Desktop initial-setup-ks.cfg Music Templates
lbdir lbdir1 Pictures Videos [root@localhost ~]#

Removing an Empty directory
#rmdir dirname

[root@localhost ~]# rmdir lbdir1
[root@localhost ~]# ls
anaconda-ks.cfg Downloads linuxfile Public Documents
Desktop initial-setup-ks.cfg Music Templates
lbdir Pictures Videos [root@localhost ~]# rmdir lbdir
rmdir: failed to remove ‘lbdir’: Directory not empty
[root@localhost ~]# ls lbdir
file1 file2 lbdir1
[root@localhost ~]#

Removing a directory with files or directories inside
A dir which is having some contents inside it cannot be removed by rmdir command. There are two ways to delete the directory with contents.

  1. Remove the contents inside the directory and then run rmdir command
  2. Run #rm –rf dirname (where r stands for recursive and f stands for forcefully.
[root@localhost ~]# rm -rf lbdir
[root@localhost ~]# ls
anaconda-ks.cfg Downloads linuxfile Public Documents Pictures
Desktop initial-setup-ks.cfg Music Templates Videos [root@localhost ~]#

Leave a Reply

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