Chapter 4 Basic Commands 1) ls 2) mkdir 3) cd 4) rmdir 5) rm 6) cp 7) mv 8) touch 9) echo 10) head 11) tail 12) less 13) more 14) grep 15) find 16) man 17) wc 18) | pipe 19) Cat 1) ls command ls command is used to list directory contents. It gives information about files. Syntax ls [option] ---[FILE]----- Options -a It display all the files including hidden files which is start with . -l long listing format about contents in the current directory. Display files in alphabetical order from a ---to---z. -r It display files in reverse order...for example from z to a order. -t sort by modification time....newest first. -R list subdirectories recursively.
-i inode (index number). Examples 1) ls command with no option 2) ls -l long listing . It gives details information about files and directories in current directory.  1st Character – File Type: First character specifies the type of the file. In the example above the hyphen (-) in the 1st character indicates that this is a normal file. Following are the possible file type options in the 1st character of the ls -l output.  Field Explanation  – normal file  d directory  s socket file  l link file  character file  block file  Field 1 – File Permissions: Next 9 character specifies the files permission. Each 3 characters refers to the read, write, execute permissions for user, group and other.  Field 2 – Number of links: Second field specifies the number of links for that file. File has a link count 1 by default. If file has a hard link,its link count will be 2. Default directory has 2 link count.
It increment as per subdirectories contain in specified directory.  Field 3 – Owner: Third field specifies owner of the file.  Field 4 – Group: Fourth field specifies the group of the file.  Field 5 – Size: Fifth field specifies the size of file.  Field 6 – Last modified date & time: Sixth field specifies the date and time of the last modification of the file.  Field 7 – File name: The last field is the name of the file. 3) ls -lr 4)ls -lt , ls –t
ls –la display all files including hidden files 5. ls –lR ls –lR command is used to show the contents in the current directory and Subdirectories.
ls –li command. An inode is an entry in inode table, containing information ( the metadata ) about a regular file and directory. An inode is a data structure on a traditional Unix-style file system such as ext3 or ext4.
2) mkdir command mkdir command is used to create a directory Syntax mkdir [option] ----DIRECTORY------ Option -m set file mode -p make parent directory as needed -v print message for each created directory. Examples: 1) Create multiple directory 2) To create parent directories using the mkdir command pass the -p option.
We can also create parent directory as per below How to create a multiple 10 directories
3) cd command is used to change directory. It is stands for change directory. Examples i) change a directory to /usr/local ii) Change from current working directory to /usr/local/lib using relative path. Working directory is /usr/local
iii) Change from current directory to /usr/local/lib using absolute path. iv) Move one directory back
v) One directory back vi) Move two directory up from where you are now. vii) Show last working directory from where we moved (use ‘–‘ switch) as shown OR viii) Move to users home directory from anywhere.
4) rmdir command rmdir command is used to delete the directories which is empty. (only empty directories). Syntac : rmdir [OPTION]... DIRECTORY... 1. To ensure that you are deleting an empty directory you should use rmdir command. If there is any files / directories in that directory it will display the following error. Now We need to delete the contents from student1 direcory,then we will be able to delete the student1 directory. 2. How to Delete Nested Empty Directories in Linux?
In this scenario, test3 directory will delete first,then test2 and last test1 3. When the directory is not empty. We can say that test3 , test2 direcory deleted,but test1 direcory has a contents.So system is unable to delete that directory. 5) rm command rm command is used to remove files and directories.
Syntax : rm [OPTION]... FILE/directory... Options: -f forcefully -i interactively -r recursively For root /superuser rm = rm –i alias is created by default in /root/.bashrc file. Examples 1) delete files with no option 2) delete multiple files 3) rm command with –f option. It will not ask for confirmation before deleting any file or directory. 4)
5) How to delete the file forcefully using relative path : 6) rm command to delete directory which contain directory. We are not able to delete directory using rm command with option -f . 7) Some times you may want to delete directory which has contents in it. You can do it with rm command as shown below. rm –r directoryname. This command will ask for confirmation before deleting the content in directory and directory.
rm –rf command It will not ask for confirmation before deleting contents in the file and directory. Don’t use this command at primary level. This will delete the directory including all the files and sub-directories. This is very dangerous when you use it accidentally as you cannot recover those files easily. So it is strongly recommended that you pay attention and think twice before executing the rm -rf command. For root /superuser rm = rm –i alias is created by default in /root/.bashrc file. So every time run rm command as such as rm filename ---It is asking for confirmation/ interactively.
Normal User Example to delete the files. User can delete only its own file. 1) Create Normal user student4 using useradd command. rm command with no option. 2) rm with –i option 3) rm –f it will not ask for confirmation 4) rm –r it will delete files recursively. 5) rm –fr it will delete the files or directory recursively.
cat Command: cat command is used to display file contents, create one or more files and concatenates files and redirect output in file. Syntax : cat [OPTION] ---file-- 1. Display contents of file 2. View Contents of Multiple files. Cat file1.txt file2.txt 3. Create a New File. After Typing Line in a File, At last press Ctl +d to save contents in a file. 4. Sometimes you may have to append the content to a file, use >> redirection symbol as shown below. To verify: 5. Copy File Content
Contents is copied from cat_example.txt file to copycontent.txt file. 6. Concatenate Contents of Multiple Files Through cat command, you will be able to concatenate contents of more than one file into a new file. We are concatenating the contents of cat_example.txt and copycontent.txt file to concatenate_contents.txt file. Pipes (|) Pipes are used to redirect a stream from one program to another. When a program's standard output is sent to another through a pipe, the first program's data, which is received by the second program, will not be displayed on the terminal. Only the filtered data returned by the second program will be displayed. The Linux pipe is represented by a vertical bar. *|* 1. An example of a command using a pipe: ls | less
This takes the output of ls, which displays the contents of your current directory, and pipes it to the less program. less displays the data sent to it one line at a time. ls normally displays directory contents across multiple rows. When you run it through less, each entry is placed on a new line. 2. To count the files in the current directory, use the command: 3. To count file in /etc/sysconfig/iptables file using pipe.
cp Command: cp command is used to copy one or more files or directories from source to destination. Syntax cp [OPTION]... SOURCE... DIRECTORY -r , -R copy directories recursively -p same as --preserve=mode, ownership, timestamps 1) cp command with no option i) to take a backup of the file
Take a backup of copy1.txt file Check a result.
2) The following example copies the file from project/backup1.txt to projectbackup/backup1_new.txt 3) If you want to copy a file from one folder to another with the same name, just the destination directory name is good enough as shown below. 4) Preserver permission while copying from source to destination. Normally ownership, timestamp changed after copying files without option –p. -p preserve permission. CP command without -p CP command with option –p. Check Time Stamp value.
Check Ownership, Timestamp : cp command without –p. Ownership and Timestamp is different after cp command without –p option. Check below output : Destination file Owner,Group and timestamp is same as per Source file. 4) A directory (and all its content) can be copied from source to destination with the recursive option -r as shown below: To preserve permission use option –p while coping. 5) Copy multiple files or directories
To copy directory, we must use –r option. Otherwise It will show omitting directory error as per below. 6) Preserve the links while copying Use option –d 7) Don’t overwrite an existing file cp –n option 8) Confirm before overwriting (interactive mode) Cp –i.
mv command mv command is used to move files and directories from one location to another.It is also used to rename file / directories. 1) rename a file. 2) rename directory. For mv command alias mv=mv –i is set by default. So while overwriting a file or directory, It always prompt you as below. 3) Prompt for a Confirmation Before Overwriting. By default mv command will not ask for any confirmation if the destination file exist, it simply overwrites it. To avoid this you might want to get a confirmation from move command before overwriting the destination file using -i option as shown below. You may type either ‘y’ or ‘n’ to accept or reject the move operation.
4) Move one or more directory to specific destination directory. Move 2 or more directory in /tmp 5) move file or multiple files in specifc destination.
Touch Command: touch command is used to create(empty file) , change and modify timestamps of a file. 1) Create a new Empty file (0 Byte File). Size of the file is O KB. 2) How to Create Multiple Files. 3) How toChange File Access and Modification Time. Stat command is used todisplay status of particular file or file systems. atime – File Access Time Access time shows the last time the data from a file was accessed – read by one of the Unix processes directly or through commands and scripts. ctime – File Change Time ctime also changes when you change file's ownership or access permissions. It will also naturally highlight the last time file had its contents updated.
mtime – File Modify Time Last modification time shows time of the last change to file's contents. It does not change with owner or permission changes, and is therefore used for tracking the actual changes to data of the file itself. To change or update the last access and modification times of a file called leena, use the -a option as follows. The following command sets the current time and date on a file. If the leena file does not exist, it will create the new empty file with the name. Check after touch –a command Access and Change time is changed. 4) How to change file modification time: After touch –m command verify Modify and Change time
5) How touse timestamp of another file (-r option). The following touch command with -r option, will update the time-stamp of file student12 with the time-stamp of student1 file. So, both the file holds the same time stamp. echo command: echo command is used to display text/strings on standard output or file. It is mostly used in shell scripting. Syntax : echo [options] [strings] 1) Input a line of text and display on standard output Output the Following text:
2) Declare a variable and echo its value. For example, Declare a variable of x and assign its value=10. Echo x value Note: The ‘-e‘ option in Linux acts as interpretation of escaped characters that are backslashed. 3) Using option ‘b‘ – backspace with backslash interpretor ‘-e‘ which removes all the spaces in between. 4) ‘n‘ – New line with backspace. 5) ‘t‘ – horizontal tab. 6) ‘v - vertical tab. grep command: grep command is used to print lines matching a pattern. Syntax : grep [OPTIONS] PATTERN [FILE...] Create grep_demo.txt file and add below contents in file THIS LINE IS THE 1ST UPPER CASE LINE IN THIS FILE. this line is the 1st lower case line in this file. This Line Has All Its First Character Of The Word With Upper Case. Two lines above this line is empty. And this is the last line.
1. Search for the given string in a single file The basic usage of grep command is to search for a specific string in the specified file as shown below. By default grep command Syntax : grep “string/pattern” filename 2. Case insensitive search using grep –i This is also a basic usage of the grep. This searches for the given string/pattern case insensitively. So it matches all the words such as “the”, “THE” and “The” case insensitively as shown below.
3. Display the lines which does not matche the given pattern. 4. Displaying lines after/before/around the match using grep -A, -B and –C. When doing a grep on a huge file, it may be useful to see some lines after the match. You might feel handy if grep can show you not only the matching lines but also the lines after/before/around the match. After Word. grep -A <N> "string" FILENAME
Before Word. Synatx grep -B <N> "string" FILENAME Around Option. –C option. This options shows N lines in both the side(before & after) of match. Synatx grep -C <N> "string" FILENAME
5. Show line number while displaying the output using grep -n 6. Searching in all files recursively using grep -r 7. When you want to count that how many lines matches the given pattern/string, then use the option -c.
WC command: wc command is used to count word, lines and bytes in a file. Syntax : wc [OPTION] .... FILE... Options -c, print the byte counts -m, print the character counts -l, print the newline counts -w, print the word counts Examples: head comamnd head command is used to prints the first N number of data of the given (file). By default, it prints first 10 lines of each given file. Syntax : head [option] filename Option –n Prints first N lines; Example 1. Print the first N number of lines
2. Print N number of lines by specifying N with – You don’t even need to pass the -n option as an argument, simply specify the N number of lines followed by ‘-‘ as shown below.
3. Print all but not the last N lines By placing ‘-‘ in front of the number with -n option, it prints all the lines of each file but not the last N lines as shown below, For Test we are redirecting first 20 lines to /etc/head_test.txt Last 5lines will not be display as below. Verify :
Tail Command Tail command is used to prints the last N number of data of the given (file). By default, it prints last 10 lines of each given file. Syntax : tail [option] filename Option –n Prints last N lines; -f To print appended data as and when the file grows Example: 1. To print last 5 lines 2. Print N number of lines by specifying N with – You don’t even need to pass the -n option as an argument, simply specify the N numberof lines followed by ‘-‘ as shown below. 3. Print the appended lines as and when the file grows
You can use -f option to output the appended lines of file instantly as shown below. More Command Displays file contents one screen at a time. It shows page in %. The command-syntax is: more [options] [file_name] 1. more We see that an indicator on the bottom left. It show us number 78%. It means that the file is long enough, so the first page is 78% of the total page. To scroll down, use space button. Then we see that the indicator number is increased. 2. In order to display a file staring at a specific line number use the following syntax:
3. Searchstring If your log file is long enough, it’s not easy to find a string that you want. Search string can help you. Using +/string can search a string for you and put the keyword on the beginning of the line. Let say we want to search “root” in /etc/passwd file. Second Option for more +/
Less command Similar to more, less command allows you to view the contents of a file and navigate through file. The main difference between more and less is that less command is faster
because it does not load the entire file at once and allows navigation though file using page up/down keys. Syntax : less [option] filename Forward Search / – search for a pattern which will take you to the next occurrence. n – for next match in forward N – for previous match in backward G - go to end of file g - go to beginning of file q - to exit from file Screen Navigation * CTRL+D – forward half window * CTRL+U – backward half window * CTRL+F – forward one window * CTRL+B – backward one window 1. less command
2. In order to display a file staring at a specific line number use the following syntax: 3. To open a file at the first occurrence of a pattern use the following syntax:
Chapter 4 Linux Basic Commands
Chapter 4 Linux Basic Commands
Chapter 4 Linux Basic Commands
Chapter 4 Linux Basic Commands
Chapter 4 Linux Basic Commands

Chapter 4 Linux Basic Commands

  • 1.
    Chapter 4 BasicCommands 1) ls 2) mkdir 3) cd 4) rmdir 5) rm 6) cp 7) mv 8) touch 9) echo 10) head 11) tail 12) less 13) more 14) grep 15) find 16) man 17) wc 18) | pipe 19) Cat 1) ls command ls command is used to list directory contents. It gives information about files. Syntax ls [option] ---[FILE]----- Options -a It display all the files including hidden files which is start with . -l long listing format about contents in the current directory. Display files in alphabetical order from a ---to---z. -r It display files in reverse order...for example from z to a order. -t sort by modification time....newest first. -R list subdirectories recursively.
  • 2.
    -i inode (indexnumber). Examples 1) ls command with no option 2) ls -l long listing . It gives details information about files and directories in current directory.  1st Character – File Type: First character specifies the type of the file. In the example above the hyphen (-) in the 1st character indicates that this is a normal file. Following are the possible file type options in the 1st character of the ls -l output.  Field Explanation  – normal file  d directory  s socket file  l link file  character file  block file  Field 1 – File Permissions: Next 9 character specifies the files permission. Each 3 characters refers to the read, write, execute permissions for user, group and other.  Field 2 – Number of links: Second field specifies the number of links for that file. File has a link count 1 by default. If file has a hard link,its link count will be 2. Default directory has 2 link count.
  • 3.
    It increment asper subdirectories contain in specified directory.  Field 3 – Owner: Third field specifies owner of the file.  Field 4 – Group: Fourth field specifies the group of the file.  Field 5 – Size: Fifth field specifies the size of file.  Field 6 – Last modified date & time: Sixth field specifies the date and time of the last modification of the file.  Field 7 – File name: The last field is the name of the file. 3) ls -lr 4)ls -lt , ls –t
  • 4.
    ls –la displayall files including hidden files 5. ls –lR ls –lR command is used to show the contents in the current directory and Subdirectories.
  • 5.
    ls –li command. Aninode is an entry in inode table, containing information ( the metadata ) about a regular file and directory. An inode is a data structure on a traditional Unix-style file system such as ext3 or ext4.
  • 6.
    2) mkdir command mkdircommand is used to create a directory Syntax mkdir [option] ----DIRECTORY------ Option -m set file mode -p make parent directory as needed -v print message for each created directory. Examples: 1) Create multiple directory 2) To create parent directories using the mkdir command pass the -p option.
  • 7.
    We can alsocreate parent directory as per below How to create a multiple 10 directories
  • 8.
    3) cd commandis used to change directory. It is stands for change directory. Examples i) change a directory to /usr/local ii) Change from current working directory to /usr/local/lib using relative path. Working directory is /usr/local
  • 9.
    iii) Change fromcurrent directory to /usr/local/lib using absolute path. iv) Move one directory back
  • 10.
    v) One directoryback vi) Move two directory up from where you are now. vii) Show last working directory from where we moved (use ‘–‘ switch) as shown OR viii) Move to users home directory from anywhere.
  • 11.
    4) rmdir command rmdircommand is used to delete the directories which is empty. (only empty directories). Syntac : rmdir [OPTION]... DIRECTORY... 1. To ensure that you are deleting an empty directory you should use rmdir command. If there is any files / directories in that directory it will display the following error. Now We need to delete the contents from student1 direcory,then we will be able to delete the student1 directory. 2. How to Delete Nested Empty Directories in Linux?
  • 12.
    In this scenario,test3 directory will delete first,then test2 and last test1 3. When the directory is not empty. We can say that test3 , test2 direcory deleted,but test1 direcory has a contents.So system is unable to delete that directory. 5) rm command rm command is used to remove files and directories.
  • 13.
    Syntax : rm[OPTION]... FILE/directory... Options: -f forcefully -i interactively -r recursively For root /superuser rm = rm –i alias is created by default in /root/.bashrc file. Examples 1) delete files with no option 2) delete multiple files 3) rm command with –f option. It will not ask for confirmation before deleting any file or directory. 4)
  • 14.
    5) How todelete the file forcefully using relative path : 6) rm command to delete directory which contain directory. We are not able to delete directory using rm command with option -f . 7) Some times you may want to delete directory which has contents in it. You can do it with rm command as shown below. rm –r directoryname. This command will ask for confirmation before deleting the content in directory and directory.
  • 15.
    rm –rf commandIt will not ask for confirmation before deleting contents in the file and directory. Don’t use this command at primary level. This will delete the directory including all the files and sub-directories. This is very dangerous when you use it accidentally as you cannot recover those files easily. So it is strongly recommended that you pay attention and think twice before executing the rm -rf command. For root /superuser rm = rm –i alias is created by default in /root/.bashrc file. So every time run rm command as such as rm filename ---It is asking for confirmation/ interactively.
  • 16.
    Normal User Exampleto delete the files. User can delete only its own file. 1) Create Normal user student4 using useradd command. rm command with no option. 2) rm with –i option 3) rm –f it will not ask for confirmation 4) rm –r it will delete files recursively. 5) rm –fr it will delete the files or directory recursively.
  • 17.
    cat Command: cat commandis used to display file contents, create one or more files and concatenates files and redirect output in file. Syntax : cat [OPTION] ---file-- 1. Display contents of file 2. View Contents of Multiple files. Cat file1.txt file2.txt 3. Create a New File. After Typing Line in a File, At last press Ctl +d to save contents in a file. 4. Sometimes you may have to append the content to a file, use >> redirection symbol as shown below. To verify: 5. Copy File Content
  • 18.
    Contents is copiedfrom cat_example.txt file to copycontent.txt file. 6. Concatenate Contents of Multiple Files Through cat command, you will be able to concatenate contents of more than one file into a new file. We are concatenating the contents of cat_example.txt and copycontent.txt file to concatenate_contents.txt file. Pipes (|) Pipes are used to redirect a stream from one program to another. When a program's standard output is sent to another through a pipe, the first program's data, which is received by the second program, will not be displayed on the terminal. Only the filtered data returned by the second program will be displayed. The Linux pipe is represented by a vertical bar. *|* 1. An example of a command using a pipe: ls | less
  • 19.
    This takes theoutput of ls, which displays the contents of your current directory, and pipes it to the less program. less displays the data sent to it one line at a time. ls normally displays directory contents across multiple rows. When you run it through less, each entry is placed on a new line. 2. To count the files in the current directory, use the command: 3. To count file in /etc/sysconfig/iptables file using pipe.
  • 22.
    cp Command: cp commandis used to copy one or more files or directories from source to destination. Syntax cp [OPTION]... SOURCE... DIRECTORY -r , -R copy directories recursively -p same as --preserve=mode, ownership, timestamps 1) cp command with no option i) to take a backup of the file
  • 23.
    Take a backupof copy1.txt file Check a result.
  • 24.
    2) The followingexample copies the file from project/backup1.txt to projectbackup/backup1_new.txt 3) If you want to copy a file from one folder to another with the same name, just the destination directory name is good enough as shown below. 4) Preserver permission while copying from source to destination. Normally ownership, timestamp changed after copying files without option –p. -p preserve permission. CP command without -p CP command with option –p. Check Time Stamp value.
  • 25.
    Check Ownership, Timestamp: cp command without –p. Ownership and Timestamp is different after cp command without –p option. Check below output : Destination file Owner,Group and timestamp is same as per Source file. 4) A directory (and all its content) can be copied from source to destination with the recursive option -r as shown below: To preserve permission use option –p while coping. 5) Copy multiple files or directories
  • 26.
    To copy directory,we must use –r option. Otherwise It will show omitting directory error as per below. 6) Preserve the links while copying Use option –d 7) Don’t overwrite an existing file cp –n option 8) Confirm before overwriting (interactive mode) Cp –i.
  • 27.
    mv command mv commandis used to move files and directories from one location to another.It is also used to rename file / directories. 1) rename a file. 2) rename directory. For mv command alias mv=mv –i is set by default. So while overwriting a file or directory, It always prompt you as below. 3) Prompt for a Confirmation Before Overwriting. By default mv command will not ask for any confirmation if the destination file exist, it simply overwrites it. To avoid this you might want to get a confirmation from move command before overwriting the destination file using -i option as shown below. You may type either ‘y’ or ‘n’ to accept or reject the move operation.
  • 28.
    4) Move oneor more directory to specific destination directory. Move 2 or more directory in /tmp 5) move file or multiple files in specifc destination.
  • 29.
    Touch Command: touch commandis used to create(empty file) , change and modify timestamps of a file. 1) Create a new Empty file (0 Byte File). Size of the file is O KB. 2) How to Create Multiple Files. 3) How toChange File Access and Modification Time. Stat command is used todisplay status of particular file or file systems. atime – File Access Time Access time shows the last time the data from a file was accessed – read by one of the Unix processes directly or through commands and scripts. ctime – File Change Time ctime also changes when you change file's ownership or access permissions. It will also naturally highlight the last time file had its contents updated.
  • 30.
    mtime – FileModify Time Last modification time shows time of the last change to file's contents. It does not change with owner or permission changes, and is therefore used for tracking the actual changes to data of the file itself. To change or update the last access and modification times of a file called leena, use the -a option as follows. The following command sets the current time and date on a file. If the leena file does not exist, it will create the new empty file with the name. Check after touch –a command Access and Change time is changed. 4) How to change file modification time: After touch –m command verify Modify and Change time
  • 31.
    5) How tousetimestamp of another file (-r option). The following touch command with -r option, will update the time-stamp of file student12 with the time-stamp of student1 file. So, both the file holds the same time stamp. echo command: echo command is used to display text/strings on standard output or file. It is mostly used in shell scripting. Syntax : echo [options] [strings] 1) Input a line of text and display on standard output Output the Following text:
  • 32.
    2) Declare avariable and echo its value. For example, Declare a variable of x and assign its value=10. Echo x value Note: The ‘-e‘ option in Linux acts as interpretation of escaped characters that are backslashed. 3) Using option ‘b‘ – backspace with backslash interpretor ‘-e‘ which removes all the spaces in between. 4) ‘n‘ – New line with backspace. 5) ‘t‘ – horizontal tab. 6) ‘v - vertical tab. grep command: grep command is used to print lines matching a pattern. Syntax : grep [OPTIONS] PATTERN [FILE...] Create grep_demo.txt file and add below contents in file THIS LINE IS THE 1ST UPPER CASE LINE IN THIS FILE. this line is the 1st lower case line in this file. This Line Has All Its First Character Of The Word With Upper Case. Two lines above this line is empty. And this is the last line.
  • 33.
    1. Search forthe given string in a single file The basic usage of grep command is to search for a specific string in the specified file as shown below. By default grep command Syntax : grep “string/pattern” filename 2. Case insensitive search using grep –i This is also a basic usage of the grep. This searches for the given string/pattern case insensitively. So it matches all the words such as “the”, “THE” and “The” case insensitively as shown below.
  • 34.
    3. Display thelines which does not matche the given pattern. 4. Displaying lines after/before/around the match using grep -A, -B and –C. When doing a grep on a huge file, it may be useful to see some lines after the match. You might feel handy if grep can show you not only the matching lines but also the lines after/before/around the match. After Word. grep -A <N> "string" FILENAME
  • 35.
    Before Word. Synatx grep-B <N> "string" FILENAME Around Option. –C option. This options shows N lines in both the side(before & after) of match. Synatx grep -C <N> "string" FILENAME
  • 36.
    5. Show linenumber while displaying the output using grep -n 6. Searching in all files recursively using grep -r 7. When you want to count that how many lines matches the given pattern/string, then use the option -c.
  • 37.
    WC command: wc commandis used to count word, lines and bytes in a file. Syntax : wc [OPTION] .... FILE... Options -c, print the byte counts -m, print the character counts -l, print the newline counts -w, print the word counts Examples: head comamnd head command is used to prints the first N number of data of the given (file). By default, it prints first 10 lines of each given file. Syntax : head [option] filename Option –n Prints first N lines; Example 1. Print the first N number of lines
  • 38.
    2. Print Nnumber of lines by specifying N with – You don’t even need to pass the -n option as an argument, simply specify the N number of lines followed by ‘-‘ as shown below.
  • 39.
    3. Print allbut not the last N lines By placing ‘-‘ in front of the number with -n option, it prints all the lines of each file but not the last N lines as shown below, For Test we are redirecting first 20 lines to /etc/head_test.txt Last 5lines will not be display as below. Verify :
  • 40.
    Tail Command Tail commandis used to prints the last N number of data of the given (file). By default, it prints last 10 lines of each given file. Syntax : tail [option] filename Option –n Prints last N lines; -f To print appended data as and when the file grows Example: 1. To print last 5 lines 2. Print N number of lines by specifying N with – You don’t even need to pass the -n option as an argument, simply specify the N numberof lines followed by ‘-‘ as shown below. 3. Print the appended lines as and when the file grows
  • 41.
    You can use-f option to output the appended lines of file instantly as shown below. More Command Displays file contents one screen at a time. It shows page in %. The command-syntax is: more [options] [file_name] 1. more We see that an indicator on the bottom left. It show us number 78%. It means that the file is long enough, so the first page is 78% of the total page. To scroll down, use space button. Then we see that the indicator number is increased. 2. In order to display a file staring at a specific line number use the following syntax:
  • 42.
    3. Searchstring If yourlog file is long enough, it’s not easy to find a string that you want. Search string can help you. Using +/string can search a string for you and put the keyword on the beginning of the line. Let say we want to search “root” in /etc/passwd file. Second Option for more +/
  • 43.
    Less command Similar tomore, less command allows you to view the contents of a file and navigate through file. The main difference between more and less is that less command is faster
  • 44.
    because it doesnot load the entire file at once and allows navigation though file using page up/down keys. Syntax : less [option] filename Forward Search / – search for a pattern which will take you to the next occurrence. n – for next match in forward N – for previous match in backward G - go to end of file g - go to beginning of file q - to exit from file Screen Navigation * CTRL+D – forward half window * CTRL+U – backward half window * CTRL+F – forward one window * CTRL+B – backward one window 1. less command
  • 45.
    2. In orderto display a file staring at a specific line number use the following syntax: 3. To open a file at the first occurrence of a pattern use the following syntax: