Home » Linux

Linux Terminal Commands - Files related commands

Working with files (file, touch, rm, cp, mv, rename, cat, tac, head, tail)

command description example
file This command shows the file type (what is the extension and which type of file is this?).
ih@linux:~$ file /home/ih/prg/ok.c
ok.c: ASCII C program text

ih@linux~# file /dev/sda
/dev/sda: block special
touch This command is used to create an empty file.
ih@linux:~$ touch ok1.txt
ih@linux:~$ touch ok2.txt
ih@linux:~$ ls -l
-rw-r--r-- 1 ih ih 31 Mar 15 12:52 ok1.txt
-rw-r--r-- 1 ih ih 31 Mar 15 12:53 ok2.txt
cat This command is used to display the file contents.
ih@linux:~$ cat /home/ih/prg/ok.c
--- file contents will display here---
head This command is used to display the first 10 lines of the file.
ih@linux:~$ head /home/ih/prg/ok.c
--- first 10 lines will display here---
tail This command is used to display last 10 lines of the file.
ih@linux:~$ tail /home/ih/prg/ok.c
--- last 10 lines will display here---
tac this command is used to display file's contents in reverse order.
ih@linux:~$ cat /home/ih/prg/ok.c
--line 1--
--line 2--
--line 3--
--line 4--

ih@linux:~$ tac /home/ih/prg/ok.c
--line 4--
--line 3--
--line 2--
--line 1--
more This command is used to display those file's conents which are more than one screen. Press space bar to display next screen & press "q" to quit the file.
ih@linux:~$ more /home/ih/prg/ok.c
--line 1--
--line 2--
--line 3--
--line 4--
.
.
.
"press space bar to display next screen".
rm this command is used to remove file(s).
ih@linux:~$ rm /home/ih/ok.c
rm -i To prevent accidental removal, this command asks before removal file.
ih@linux:~$ rm -i /home/ih/ok.c
rm: remove regular file `ok.c'? yes
rm -rf Here “r” stands for recursive, and “f” stands for force. This command is used to remove everything from the given directory. However rm does not delete the directory’s all file(s)/ directory(s). Use rm –rf to remove everything.
ih@linux:~$ rm /home/ih
rm: cannot remove `ih': Is a directory

ih@linux:~$ rm -rf /home/ih
ih@linux:~$ ls
ls: cannot access ih: No such file or directory
cp This command is used to copy one file.
ih@linux:~$ cp /home/ih/ok.c  okTemp.c
ih@linux:~$ ls
ok.c  okTemp.c
cp -i This command is used to prevent overwriting of existing file.
ih@linux:~$ cp /home/ih/ok.c  ok.c
cp: overwrite file ‘ok.c’: n
cp -r This command is used to copy all files & subdirectories of the directory.
ih@linux:~$ cp –r /home/ih  /home/backup
all files and subdirectories of directory 
“/home/ih” will be copied into “/home/backup”
mv This command is used to move or rename a file.
ih@linux:~$ ls
ok.c  okTemp.c

ih@linux:~$ mv  /home/ih/ok.c  xyz.c

ih@linux:~$ ls
okTemp.c  xyz.c
mv -i This command is used to prevent overwriting of existing file while moving or renaming
ih@linux:~$ ls
ok.c  okTemp.c

ih@linux:~$ mv  /home/ih/ok.c  okTemp.c
mv: overwrite ` okTemp.c'? n

ih@linux:~$ ls
ok.c  okTemp.c




Comments and Discussions!










Copyright © 2023 www.includehelp.com. All rights reserved.