Linux Commands – File Operations – Continued


Renaming files

There is no special command to rename a file or directory. But we can use mv to do the same.

Copying and Removing directories

Using -R option we can apply cp and rm to directories also.

Using -f in case of rm forces the delete.

Example


vishnu@bios-11:~$ ls
Desktop    Downloads         Music     Public     Ubuntu One
Documents  examples.desktop  Pictures  Templates  Videos
vishnu@bios-11:~$ mkdir test
vishnu@bios-11:~$ ls
Desktop    Downloads         Music     Public     test        Videos
Documents  examples.desktop  Pictures  Templates  Ubuntu One
vishnu@bios-11:~$ mv test/ temp
vishnu@bios-11:~$ ls
Desktop    Downloads         Music     Public  Templates   Videos
Documents  examples.desktop  Pictures  temp    Ubuntu One
vishnu@bios-11:~$ cp -R temp Documents/
vishnu@bios-11:~$ ls
Desktop    Downloads         Music     Public  Templates   Videos
Documents  examples.desktop  Pictures  temp    Ubuntu One
vishnu@bios-11:~$ rm temp
rm: cannot remove `temp': Is a directory
vishnu@bios-11:~$ rm -Rf temp
vishnu@bios-11:~$ ls
Desktop    Downloads         Music     Public     Ubuntu One
Documents  examples.desktop  Pictures  Templates  Videos
vishnu@bios-11:~$ cd ./Documents/
vishnu@bios-11:~/Documents$ ls
blog  docs  learn  task list  temp
vishnu@bios-11:~/Documents$ rmdir temp
vishnu@bios-11:~/Documents$ ls
blog  docs  learn  task list
There are a lot of options for almost every command. So it is a good practise to read the man page of every new command we learn 🙂

Linux Commands – File Operations


Command : cp <source> <destination>

What it does: Copies source file or directory to destination.

Command : mv <source> <destination>

What it does: Moves source file or directory to destination.

Command : rm <file>

What it does: Removes the file or directory.

Command : mkdir <dir>

What it does : Makes a directory named <dir>.

Command : rmdir <dir>

What it does: Removes <dir> if empty.

Example


bios@bios-mac-7:~$ cd ./Vishnu\ RKM/
bios@bios-mac-7:~/Vishnu RKM$ ls
error.png       plugin prob.odt        terminal_eror.png
lock error.png  suggested plugins.png
bios@bios-mac-7:~/Vishnu RKM$ mkdir backup
bios@bios-mac-7:~/Vishnu RKM$ ls
backup     lock error.png   suggested plugins.png
error.png  plugin prob.odt  terminal_eror.png
bios@bios-mac-7:~/Vishnu RKM$ cp plugin\ prob.odt ./backup/
bios@bios-mac-7:~/Vishnu RKM$ ls
backup     lock error.png   suggested plugins.png
error.png  plugin prob.odt  terminal_eror.png
bios@bios-mac-7:~/Vishnu RKM$ cd ./backup/
bios@bios-mac-7:~/Vishnu RKM/backup$ ls
plugin prob.odt
bios@bios-mac-7:~/Vishnu RKM/backup$ cd ./..
bios@bios-mac-7:~/Vishnu RKM$ rmdir backup/
rmdir: failed to remove `backup/': Directory not empty
bios@bios-mac-7:~/Vishnu RKM$ cd ./backup/
bios@bios-mac-7:~/Vishnu RKM/backup$ rm plugin\ prob.odt
bios@bios-mac-7:~/Vishnu RKM/backup$ ls
bios@bios-mac-7:~/Vishnu RKM/backup$ cd ./..
bios@bios-mac-7:~/Vishnu RKM$ rmdir backup/
bios@bios-mac-7:~/Vishnu RKM$ ls
error.png       plugin prob.odt        terminal_eror.png
lock error.png  suggested plugins.png
bios@bios-mac-7:~/Vishnu RKM$ mkdir pics
bios@bios-mac-7:~/Vishnu RKM$ cp *.png ./pics/
bios@bios-mac-7:~/Vishnu RKM$ rm *.png
bios@bios-mac-7:~/Vishnu RKM$ ls
pics  plugin prob.odt
bios@bios-mac-7:~/Vishnu RKM$ cd ./pics/
bios@bios-mac-7:~/Vishnu RKM/pics$ ls
error.png  lock error.png  suggested plugins.png  terminal_eror.png
bios@bios-mac-7:~/Vishnu RKM/pics$ cd ./..
bios@bios-mac-7:~/Vishnu RKM$ mkdir files
bios@bios-mac-7:~/Vishnu RKM$ ls
files  pics  plugin prob.odt
bios@bios-mac-7:~/Vishnu RKM$ mv plugin\ prob.odt ./files/
bios@bios-mac-7:~/Vishnu RKM$ ls
files  pics
bios@bios-mac-7:~/Vishnu RKM$ mkdir temp
bios@bios-mac-7:~/Vishnu RKM$ ls
files  pics  temp
bios@bios-mac-7:~/Vishnu RKM$ rmdir temp
bios@bios-mac-7:~/Vishnu RKM$ ls
files  pics