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?).
[email protected]:~$ file /home/ih/prg/ok.c
ok.c: ASCII C program text

[email protected]~# file /dev/sda
/dev/sda: block special
touch This command is used to create an empty file.
[email protected]:~$ touch ok1.txt
[email protected]:~$ touch ok2.txt
[email protected]:~$ 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.
[email protected]:~$ 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.
[email protected]:~$ 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.
[email protected]:~$ 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.
[email protected]:~$ cat /home/ih/prg/ok.c
--line 1--
--line 2--
--line 3--
--line 4--

[email protected]:~$ 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.
[email protected]:~$ 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).
[email protected]:~$ rm /home/ih/ok.c
rm -i To prevent accidental removal, this command asks before removal file.
[email protected]:~$ 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.
[email protected]:~$ rm /home/ih
rm: cannot remove `ih': Is a directory

[email protected]:~$ rm -rf /home/ih
[email protected]:~$ ls
ls: cannot access ih: No such file or directory
cp This command is used to copy one file.
[email protected]:~$ cp /home/ih/ok.c  okTemp.c
[email protected]:~$ ls
ok.c  okTemp.c
cp -i This command is used to prevent overwriting of existing file.
[email protected]:~$ 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.
[email protected]:~$ 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.
[email protected]:~$ ls
ok.c  okTemp.c

[email protected]:~$ mv  /home/ih/ok.c  xyz.c

[email protected]:~$ ls
okTemp.c  xyz.c
mv -i This command is used to prevent overwriting of existing file while moving or renaming
[email protected]:~$ ls
ok.c  okTemp.c

[email protected]:~$ mv  /home/ih/ok.c  okTemp.c
mv: overwrite ` okTemp.c'? n

[email protected]:~$ ls
ok.c  okTemp.c







Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.
Learn PCB Designing: PCB DESIGNING TUTORIAL




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.