Chapter 7.3 - Linux File Management: A Comprehensive Guide to Commands and Techniques

Managing files in Linux can seem daunting at first, but with the right knowledge and tools, it can be a seamless and efficient process. In this blog, we will explore the various file management techniques in Linux, from navigating the file system to organizing and manipulating files. Whether you're a beginner or an experienced user, this guide will help you streamline your file management tasks and make the most out of your Linux system. Let's look at some commands :

  • wc [options] [file(s)] → used to count the number of lines, words, and characters in a file.

  • cat [filename] → displlay the content of a file.

  • cat -n [filename] → display the content of a file along with number of lines.

  • less [filename] → shows the file contents one at a time.

  • head [options] [filename] → Show first few lines of a file.

  • head -20 [filename] → Show 20 lines of the file.

  • tac [filename] → It shows the file backwards (Opposite of cat).

  • touch [filename] → To create a file.

  • mkdir [dirname] → To make a directory.

  • rmdir [dirname] → To remove a directory (Empty).

Moving, Renaming or Rem :

  • mv → Rename a file or move a file to anothe location.

  • rm → Remove a file.

  • rm -f → Forcefully remove a file.

  • rm -i →Interactively remov a file.

Renaming or Removing a Directory :

  • rm -rf → Forcefully remove a directory recursively.

Standard File Streams :

When command are executed by default there are three standard file streams or descriptors always open for use:

NameSymbolic NameValueExample
Standard Inputstdin0Keyboard
Standard Outputstdout1Terminal
Standard Errorstderr2Log FIle

I/O Redirection :

Through the command shell we can redirect the three standard file stream so we can get the input from either a file or another command instead of from our keyboard and we can write output and errors to files or use them to provide input for subsequence commands. Example: If we have a program called do something that reads from stdin and writes to stdout and stderr, we can change its input source by using the less than sign followed by the name of the file to be consumed for input data. Example :

do_something < input_file

If you want to send the output to a file use the greator than sign. Example:

do_something > output_file

Because standard error is not the same as standard out, error message will still be seen in the terminal window as the above example.

If you want to redirect standard error to a seprate file you can use standard errors file descriptor number 2, the greator than sign followed by the name of the file you want to hold everything. The running command writes to standard error, so here's how you do that :

do_something 2 > error_file

A special shorthand notation can send anything written to file descriptor 2, standard error to the same place as file descriptor 1, standard out . You can do like this :

do_something > all_output_file 2 >&1

Bash permits an easier syntax for the above which is like so:

do_something > & all_output_file