Filtering IO/File

The head command in Linux is used to display the first few lines of a text file or the output of a command.

head file.txt             // display the first 10 lines of the file "file.txt"
head -n 20 file.txt       // display the first 20 lines of the file "file.txt"
head -c 100 file.txt      // display the first 100 bytes of the file "file.txt"
head -q file1.txt file2.txt file3.txt   // display the first 10 lines of each of the three files, without printing their names

tail

The tail command in Linux is used to display the last few lines of a text file or the output of a command.

tail file.txt             // display the last 10 lines of the file "file.txt"
tail -n 20 file.txt       // display the last 20 lines of the file "file.txt"
tail -f logfile.txt       // continuously display the last few lines of the file "logfile.txt" as new lines are added to it
tail -q file1.txt file2.txt file3.txt   // display the last 10 lines of each of the three files, without printing their names

grep

The grep command in Linux is used to search for a specific pattern or regular expression in a file or a stream of data.

grep 'pattern' file.txt          // Search for "pattern" in "file.txt"
grep -i 'pattern' file.txt       // Search for "pattern" in "file.txt", ignoring case
grep -w 'pattern' file.txt       // Search for the whole word "pattern" in "file.txt"
grep -v 'pattern' file.txt       // Search for lines that do not contain "pattern" in "file.txt"
grep -c 'pattern' file.txt       // Count the number of lines that contain "pattern" in "file.txt"
grep -n 'pattern' file.txt       // Show the line numbers of the lines that contain "pattern" in "file.txt"
grep -r 'pattern' /path/to/dir   // Recursively search through all files and directories in /path/to/dir for "pattern"

pipe

The pipe command in Linux can be used to chain together multiple commands to perform more complex operations on data. Here's an example of how to use the pipe command with the head and tail commands:

For example, let's say you have a file named data.txt with 100 lines of text. You want to extract lines 21 to 30 from this file. You can do this using the following command:

cat data.txt | head -n 30 | tail -n 10

find

The find command in Linux is used to search for files and directories in a directory hierarchy based on various criteria, such as name, type, size, modification time, etc. It is a very powerful command that can perform complex searches.

find . -type f -name '*.txt'     //all files with the .txt extension in the current directory and its subdirectories.
find . -type d -name "backup"    //all directories in the current directory and its subdirectories that have the name backup:

more

The more command in Linux is used to display the contents of a file one page at a time. It is often used to display large files or files that are too long to fit on a single screen.

more filename

less

The less command in Linux is similar to the more command, but it provides more advanced features and is generally considered a more powerful pager utility. Like more, less is used to display the contents of a file one page at a time.

less filename.txt

cut

The cut command in Linux is used to extract sections from each line of a file. It is particularly useful for extracting specific columns of data from a file.

cut -d' ' -f2 filename.txt

sort

The sort command in Linux is used to sort the contents of a file or the output of a command in alphabetical, numerical, or other specified order.

sort filename.txt   //sort the contents of a file in alphabetical order
sort -r filename.txt //sort the contents of a file in reverse orderba

Last updated