Working with Files

Working with Files

The cat command

The cat command concatenates and prints the contents of files.

cat filename will read the contents of the file and print them out.

For example, cat instructions.txt will read in from the instructions.txt file and then print the contents out to the screen.

cat <filename>        


If we provide cat with multiple files, it will concatenate their contents and output them.

cat peanutbutter.js jelly.css will output peanutbutter.js first and immediately after print the contents of jelly.css file.

cat  <file1>  <file2>        


The less command

The less command displays the contents of a file, one page at a time. We can navigate forwards and backwards through the file using arrow keys, which is especially useful with very large files. Press q to get out of it.

less somefile.txt will display the contents of somefile.txt using less.

less <filename>        

When viewing a file using less..

  • Press space or f to go to the next page of the file.
  • Press b to go back to the previous page
  • Press Enter or Down arrow to scroll by one line
  • To search, type forward slash / followed by a pattern
  • Press q to quit


In the example below, I have opened the bash_history file using less ~/.bash_history command.


The tac command

tac (cat spelled backwards) will concatenate and print files in reverse. It prints each line of a file, starting with the last line. You can think of it as printing in reverse "vertically".


The rev command

The rev command prints the contents of a file, reversing the order of each line. Think of it as a "horizontal" reverse, whereas tac is a "vertical" reverse.


The head and tail commands

The head command prints a portion of a file, starting from the beginning of the file. By default, it prints the first 10 lines of file.

Otherwise, you have a -n option which will print the first n lines of the file.

head warAndPeace.txt would print the first 10 lines of warAndPeace.txt file.


The tail command works similarly to the head command, except it prints from the END of a file. By default, it prints the last 10 lines of a file.

The -n option works here too. But instead of printing the first n, it prints the last n lines of the file.

tail warAndPeace.txt would print the last 10 lines of the warAndPeace.txt file.


The wc command

The word count command wc can tell us the number of words, lines, or bytes in files. By default, it prints out three numbers: the lines, words, and bytes in a file.

We can use the -l option to limit the output to the number of lines.

The -w option limits the output to the number of words of the file.

In the example below, our file readinglist.txt contains 7 lines, 17 words and 127 bytes.


The sort command

The sort command outputs the sorted contents of a file (it does not change the file itself). By default, it will sort the lines of a file alphabetically.

sort names.txt would print each line from names.txt, sorted in alphabetical order.

sort filename.txt        


The -r option tells the sort command to sort in reverse order.

sort -r names.txt would print each line from names.txt, sorted in REVERSE alphabetical order.

sort  -r  filename.txt        


We can use the -n option to sort a file containing numerical contents.

Standalone sort command would not work as we want it to, because it'll sort the contents based on the first digit only.

Let's demonstrate this with a file "prices.txt".


The -u option tells the sort command to ignore the duplicates and instead only sort the unique values.


We can specify a particular "column" that we want to sort by, using the -k option followed by a field number.

In this example, sort -nk 2 data.txt tells sort to use the numeric sort and to sort using the 2nd field.


Thanks for Reading : ). Hope you find it useful.

要查看或添加评论,请登录

Shivm Soaini的更多文章

  • Pipelines

    Pipelines

    Pipes are used to redirect a stream from one program to another program. We can take the output of one command and…

  • The Standard Streams and Redirection

    The Standard Streams and Redirection

    The three standard streams are communication channels between a computer program and its environment. They are:…

  • The Shortcuts

    The Shortcuts

    We can speed up our command-line experience by taking advantage of the many built-in shortcuts. These shortcuts are…

  • Deleting, Moving and Copying

    Deleting, Moving and Copying

    The rm command We use the rm (remove) command to remove files from our machine. For example, rm app.

  • The Nano Text editor

    The Nano Text editor

    Nano is a simple text editor that we can access right from the terminal. It's far more accessible than other popular…

  • Making Files and Folders

    Making Files and Folders

    The touch command To create a new file from the command line, use the touch command. Provide a filename, and that file…

  • Navigating the CLI Sea

    Navigating the CLI Sea

    The root directory The starting point for the file system is the root folder. We call it the root, but its actual…

  • Getting Help in Command Line Interface

    Getting Help in Command Line Interface

    Sometimes it's difficult to wrap our head around some of the commands we encounter. In this scenario, man pages can…

  • First Step in Command Line

    First Step in Command Line

    Some Familiar Terms A Shell is a computer interface to an operating system which expose the OS's services to the human…

  • A Brief History of Linux

    A Brief History of Linux

    The world of operating systems has many residents, but they can be segregated into two tribes: The Microsoft NT…

社区洞察