Useful Linux Commands — Files and Histories

John Au-Yeung - Feb 13 '21 - - Dev Community

Check out my books on Amazon at https://www.amazon.com/John-Au-Yeung/e/B08FT5NT62

Subscribe to my email list now at http://jauyeung.net/subscribe/

Linux is an operating system that many developers will use.

Therefore, it’s a good idea to learn some Linux commands.

In this article, we’ll look at some useful Linux commands we should know.

uname

The uname command lets us print details about the current machine and OS running on it.

We can use the -m switch to show the hardware name.

The -p switch prints the processor architecture.

The -s switch prints the OS name.

-r prints the release and -v prints the version.

The -n prints the node network name.

-a print prints everything.

man

man lets us open the help page for a given command.

We run man <command> to get help with a command.

Man pages are divided into 7 different groups, identified by a number:

  • 1 is user commands
  • 2 is kernel system calls
  • 3 is C library functions
  • 4 is devices
  • 5 is files formats and filesystems
  • 6 is games
  • 7 is miscellaneous commands, conventions and overviews
  • 8 is superuser and system administrator commands

grep

The grep command lets us search for text with a pattern/

For instance, we run:

grep -n document index.md
Enter fullscreen mode Exit fullscreen mode

to search for the document keyword in the index.md file.

The -n switch lets us show the line numbers of the result.

We can also use grep to filter the output of another command.

To do this, we run:

less index.md | grep -n document
Enter fullscreen mode Exit fullscreen mode

We open the index.md file with less , then we pipe the outline to the grep command to search for the word document in index.md .

Also, we can invert the result with the -v option to exclude a particular string.

umask

The umask command lets us set default permissions for files.

Running umask without arguments will show us the current mask.

The mask is the octal number representing the current permissions.

umask -S shows us the permissions with human-readable notation.

The digits return means the following:

  • 0 — read, write, execute
  • 1 — read and write
  • 2 — read and execute
  • 3 — read-only
  • 4 — write and execute
  • 5 — write-only
  • 6 — execute only
  • 7 — no permissions

We can set a new value by passing it in as an argument:

umask 002
Enter fullscreen mode Exit fullscreen mode

We can also specify the permission by role:

umask g+r
Enter fullscreen mode Exit fullscreen mode

du

We can run the du command to calculate the space usage of files and directories.

We run it to tet the list of items and their sizes in bytes.

The -a switch lets us print the size of each file in the directories.

We can sort the results with the sort :

du -h <directory> | sort -nr
Enter fullscreen mode Exit fullscreen mode

history

The history command lets us view the command line history.

We can also use !<commnand number> to repeat the command with the given number from the history output.

To clear the command history, we run history -c .

Conclusion

We can list files, search outputs, and command history with various Linux commands.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .