Managing File From Command Line In Linux

On a Linux system, everything is a file; if something is not a file, it is a process. Most files are regular fils; they contain normal data, for example text files, executable file or programs, input or output from a program and so on. As Linux stores data and program in files. These are organized in directories. A directory is just a file that contains other files or directories.

File System Structure:

In Windows operating system, we have C:\ drive, and there are many directories under C:

, such as Documents and Settings, Program Files, etc. In Linux, we can think of /(root directory) as C drive in Windows. All files system directory or mount points in Linux are under / directory. There are many directories full of child directories and files. Some directories such as bin, sbin, and lib can be found in several other directories too. The following table describes many of the most command Linux directories. Some file system and directories structure in one Linux distribution maybe different from other Linux distributions.

File System Hierarchy:

All files on a Linux system are stored on file systems which are organized into a single inverted tree or directories, known as a file system hierarchy. In the inverted tree, root lies at the top and the branches of directories and sub-directories stretch below the root.

/ Root directory of the system  
/bin Keeps binary files for user application (bash, ls, mount, tar, etc.)  
/sbin Keep static binary files for system program. (fdisk, fsck, init, etc)  
/etc Directory contains static, persistent system configuration data  
/dev Device and hardware files are store here  
/proc Virtual filesystem providing process and kernel information as files  
/var Keeps system log files
/tmp All users have permission to place temporary files here.  
/usr Keeps Linux system files installed software, shared libraries etc  
/home Location of users personal home directories and configuration  
/boot Files needed in order to start the boot process  
/lib Libraries essential for the binaries, keep System Libraries  
/opt Optional application software packages  
/mnt Temporarily mounted filesystems  
/media Mount points for removable media such as CD-ROMs  
/root The root (super-user)home directory  
/srv Service data

Locating Files by Name:

To locating a files or folders by name we can use absolute path or relative path.

Path:

A path is a unique location to a file or a folder in a file system of an OS. A path to a file is a combination of / and alpha-numeric characters.

Absolute Path:

An absolute path is define as the specifying the location of a file or directory from the root directory (/). Absolute path is a complete path from start of actual filesystem from / directory.

Such as a folder name doc on a root desktop: #cd /root/Desktop/doc

Relative Path:

Relative Path is identifies a unique file, specifying only the path necessary to reach the file from the working directory. A path name with anything other than a forward slash (/) as a first character is a relative path name. Such as a folder name doc on a root desktop: #cd doc

Navigating Paths:

Many task rely on being able to get to, or reference the correct location in the system. A lot of command on the terminal will rely on you being in the right location. A you’re moving around. The first command for Navigation around is pwd which stands for (Print Working Directory) this command just tells what your current or present working directory is. in order to move around in the system, we use a command called cd which stands for change directory.

Navigation path commands

cd        – Change Directory (change Directory)

[cms@asim ~]$ cd Desktop

cd..     – Moves one level up in the directory

[cms@asim Desktop]$ cd ..

cd /     – Moves using absolute path

[cms@asim ~]$ cd /run/media

cd~     – Change to user’s home directory

[cms@asim media]$ cd ~ [cms@asim ~]$ pwd
/home/cms

cd –     – Changes to previous working directory

[cms@asim ~]$ cd – /run/media

cd../ ..- Changes to two levels up

[cms@asim media]$ cd ../.. [cms@asim /]$

pwd    – Print Working Directory

[cms@asm ~]$ pwd /home/cms

ls         – Show you list of directory(list)

[cms@asim ~]$ ls
Desktop  Documents  Downloads  Music  Pictures  Public  Templates  Videos

ll          – Same is ls-l list conteents of directory in long format

[cms@asim ~]$ ll total 0 drwxr-xr-x. 2 cms cms 18 Jun 19 12:10 Desktop drwxr-xr-x. 2 cms cms  6 Jun 12 17:33 Documents drwxr-xr-x. 2 cms cms  6 Jun 12 17:33 Downloads drwxr-xr-x. 2 cms cms  6 Jun 12 17:33 Music drwxr-xr-x. 2 cms cms  6 Jun 12 17:33 Pictures drwxr-xr-x. 2 cms cms  6 Jun 12 17:33 Public drwxr-xr-x. 2 cms cms  6 Jun 12 17:33 Templates drwxr-xr-x. 2 cms cms  6 Jun 12 17:33 Videos

ls -r     – Reverse the sort order (r=Reverse)

[cms@asim ~]$ ls -r Videos  Templates  Public  Pictures  Music  Downloads  Documents  Desktop

ls -t     – List the most recent files first (t=time)

[cms@asim ~]$ ls -t Desktop  Documents  Downloads  Music  Pictures  Public  Templates  Videos

ls -S     – List the biggest files first (s=Size)

[cms@asim ~]$ ls -s total 0 0 Desktop    0 Downloads  0 Pictures  0 Templates 0 Documents  0 Music      0 Public    0 Videos

ls -l      – List content of directory in long format (long)

[cms@asim ~]$ ls -l total 0 drwxr-xr-x. 2 cms cms 18 Jun 19 12:10 Desktop drwxr-xr-x. 2 cms cms  6 Jun 12 17:33 Documents drwxr-xr-x. 2 cms cms  6 Jun 12 17:33 Downloads drwxr-xr-x. 2 cms cms  6 Jun 12 17:33 Music drwxr-xr-x. 2 cms cms  6 Jun 12 17:33 Pictures drwxr-xr-x. 2 cms cms  6 Jun 12 17:33 Public drwxr-xr-x. 2 cms cms  6 Jun 12 17:33 Templates drwxr-xr-x. 2 cms cms  6 Jun 12 17:33 Videos

ls -a     – List contents of directory including hidden files (All)

[cms@asim ~]$ ls -a .              .bash_logout   .cache   Desktop    .esd_auth      .mozilla  Public ..             .bash_profile  .config  Documents  .ICEauthority  Music     Templates .bash_history  .bashrc        .dbus    Downloads  .local         Pictures  Videos

ls -h    – List contents of directory in human readble (Human)

[cms@asim ~]$ ls -h Desktop  Documents  Downloads  Music  Pictures  Public  Templates  Videos

Managing files command tools

#cp     – Copy files from source to destination (copy)

[cms@asim Desktop]$ cp file1 file4

#cp-r  – Copy folder from source to destination (Recursive)

[cms@asim Desktop]$ cp -r file1 file6

#rm    – Removes files (Remove)

[cms@asim Desktop]$ rm file4

#rm -r            – Remove folder (Recursive)

[cms@asim Desktop]$ rm -r file6

#rm -f            – Remove files or folder forcefully(Forecfully)

[cms@asim Desktop]$ rm -f file2

#rm-rf           – Force to remove folder (Recursive Forcefully)

[cms@asim Desktop]$ rm -rf dir1

#mkdir          – make empty folder (Make Directory)

[cms@asim Desktop]$ mkdir dir1

#mkdir {1,2,3,4}     – Make more than one empty folders

[cms@asim Desktop]$ mkdir {1,2,3,4} [cms@asim Desktop]$ mkdir dir2 dir3 dir4

#mkdir {1..10}                    – Make sequence of files from 1 to 10

[cms@asim Desktop]$ mkdir {1..10} [cms@asim Desktop]$ ll total 4 drwxrwxr-x. 2 cms  cms   6 Jun 22 15:58 1 drwxrwxr-x. 2 cms  cms   6 Jun 22 15:58 2 drwxrwxr-x. 2 cms  cms   6 Jun 22 15:58 3 drwxrwxr-x. 2 cms  cms   6 Jun 22 15:58 4 drwxrwxr-x. 2 cms  cms   6 Jun 22 15:58 5 drwxrwxr-x. 2 cms  cms   6 Jun 22 15:58 6 drwxrwxr-x. 2 cms  cms   6 Jun 22 15:58 7 drwxrwxr-x. 2 cms  cms   6 Jun 22 15:58 8 drwxrwxr-x. 2 cms  cms   6 Jun 22 15:58 9 drwxrwxr-x. 2 cms  cms   6 Jun 22 15:58 10

#rmdir <folder>                 – Remove empty folder (Remove Directory)

[cms@asim Desktop]$ rmdir dir1

#rmdir {1..10}                     – Remove sequence from 1 to 10

[cms@asim Desktop]$ rmdir {1..10}

#mv <s><d>                        – Move or rename files or folders (Move)

[cms@asim Desktop]$ mv file3 file9

#touch                                   – Create empty files

[cms@asim Desktop]$ touch f1

#touch {1,2,3,4}     – Creating more than one empty files

[cms@asim Desktop]$ touch {1,2,3}

#cmp – Compare two files

[cms@asim Desktop]$ vim f1 [cms@asim Desktop]$ vim f2 [cms@asim Desktop]$ cmp f1 f2 f1 f2 differ: byte 1, line 1

#comm          – Displays what is command between files

[cms@asim Desktop]$ comm f1 f2 hi my name is asim             Today i will teach you chp no 2  

#dir    – Briefly list directory contents

[cms@asim Desktop]$ dir dir1 f1  touch2

#tail    – Display last few lines of a text file

[cms@asim Desktop]$ tail /etc/passwd gnome-initial-setup:x:988:982::/run/gnome-initial-setup/:/sbin/nologin sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin postfix:x:89:89::/var/spool/postfix:/sbin/nologin tcpdump:x:72:72::/:/sbin/nologin cms:x:1000:1000:cms:/home/cms:/bin/bash ldap:x:55:55:OpenLDAP server:/var/lib/ldap:/sbin/nologin ldapuser1:x:1001:1001::/home/ldapuser1:/bin/bash ldapuser2:x:1002:1002::/home/ldapuser2:/bin/bash apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin

#tail -n                       – Display last given number of line of text file (Number)

[cms@asim Desktop]$ tail -n 3 /etc/passwd

ldapuser1:x:1001:1001::/home/ldapuser1:/bin/bash ldapuser2:x:1002:1002::/home/ldapuser2:/bin/bash apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin

#head            – Display first few lines of a text file

[cms@asim Desktop]$ head /etc/passwd root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin adm:x:3:4:adm:/var/adm:/sbin/nologin lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin sync:x:5:0:sync:/sbin:/bin/sync shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown halt:x:7:0:halt:/sbin:/sbin/halt mail:x:8:12:mail:/var/spool/mail:/sbin/nologin operator:x:11:0:operator:/root:/sbin/nologin

#head -n                   – Display first given number of line of text file ( Number)

[cms@asim Desktop]$ head -5 /etc/passwd root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin adm:x:3:4:adm:/var/adm:/sbin/nologin lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin

#wc    – Count lines, Words and characters

[cms@asim Desktop]$ wc /etc/passwd   48   95 2509 /etc/passwd

#wc -l – Count lines only

[cms@asim Desktop]$ wc -l /etc/passwd 48 /etc/passwd

#wc -w          – Count words only

[cms@asim Desktop]$ wc -w /etc/passwd 95 /etc/passwd

#wc -c            – Count characters only

[cms@asim Desktop]$ wc -c /etc/passwd 2509 /etc/passwd

#cat    – Display contents of a text file

[cms@asim Desktop]$ cat f1 hi my name is asim

#cat -n filename     – To see how many lines are written in the file (Number)

[cms@asim Desktop]$ cat -n f1      1     hi my name is asim

#less   – Display the contents of a text file page wise

[cms@asim Desktop]$ less f1

#diff   – Display the difference between file

[cms@asim Desktop]$ diff f1 f2 1c1 < hi my name is asim — > Today i will teach you chp no 2

#file    – Display the information about file contents

[cms@asim Desktop]$ file f1 f1: ASCII text

Matching File Name Using Path Name Expansion:

Often, needs to perform a single operation on many filesystem objects. For example, if we needed to create multiple directories all beginning with the name “Doc” and then followed by a number, we could issue the command: mkdir Doc1 Doc2 Doc3 Doc4. it will be very quiet time consuming  if  there were a lot to create. However, we could save a lot of time by issuing the command: mkdir Doc(01..10). Basic appreciation of wildcards, pattern matching and expansion technique can rescue us.

Wildcard or Globbing:

A wildcard or Globbing (because it was originally implemented as a program called /etc/glob) is a symbol used to replace or present one or more characters. Wildcards are typically either an asterisk (*), which represents one or more characters or question mark (?), which represents a single character or Open and close brackets ([]) which match a single character in range.

Pattern Matching Characters:

f* – Only filename beginning with “f”

[cms@asim Desktop]$ ls f* f1  f2  file9

*f – Only filenames ending in “f”

[cms@asim Desktop]$ ls *f ff

*f* – Only filenames containing a “f”

[cms@asim Desktop]$ ls *f* f1  f2  ff  file9

[!f*] – Only filenames where first character is not “f”

[cms@asim Desktop]$ ls [!f*] 1: 2: 3: 4:

???* – Only filenames at least 3 characters in length

[cms@asim Desktop]$ ls ???* file9  pass 1..10: dir1: f1  touch2 dir2: dir3: dir4:

*[[:digit:]]* – Only filenames that contain a number

[cms@asim Desktop]$ ls *[[:digit:]]* b1  f1  f2  file9 1: 1..10: 2: 3: 4: dir1: f1  touch2 dir2: dir3: dir4:

Leave a Reply

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