Skip to content

Instantly share code, notes, and snippets.

@denis-mironov
Last active February 26, 2018 13:47
Show Gist options
  • Save denis-mironov/2a7901019766b0175596453e8d501bfc to your computer and use it in GitHub Desktop.
Save denis-mironov/2a7901019766b0175596453e8d501bfc to your computer and use it in GitHub Desktop.
LINUX often use commands

Unix shell commands

Main commands

  • uname -a - Linux kernel version
  • whoami - displays the username of the current user.
  • echo - output text to the screen
  • env - list environment variables. Use the dollar sign($) to refer.
  • unset - remove env variable

Info commands

  • man - manual page. man [command] opens manual for this command
  • man -k [some action] - search commands in manual for this action
  • info [command] - useful tool that you can use to find out more about Linux commands
  • history - display the list of commands that you’ve entered before
  • !string in history - run that command again
  • Ctrl+r - search in your history

Working with directories

  • cd [directory] - enter directory
  • ls - lists the directory content
  • ls -a - lists the directory hidden content
  • ls -la - lists the directory hidden content with info
  • pwd - print the path of the current directory
  • mkdir [directory] - create a new directoy
  • mv [directory] - move a directory, using with two parameters.The first paramter is the directory that you would like to move and the second paramter is the location where you would like to move the directory to.
  • rmdir [directory] - remove empty directory
  • rm -r [directory] - remove the directory and its content recursively

Working with files

  • touch [file] - the most common way to create a file in Linux. Although the primary purpose of this command is to update the timestamp of a file
  • cp [file] - copy file or directory. Using with two parameters
  • rm [file] - remove file
  • rm -f /path/to/directory/* - remove all the "files" from inside a folder(not removing interior folders)
  • rm -rf /path/to/directory/* - remove all the contents of the folder(including all interior folders) but not the folder itself
  • rm -rf /path/to/directory - remove the folder with all its contents(including all interior folders)
  • ls -LR / ls * shows the files recursively in this directory
  • grep some_text path/to/file.txt - search the file.txt for for each occurence of the some_text
  • grep -r some_text path/to/folder/* - search the directory path/to/folder/ for each occurence of the word some_text
  • grep -i - case-insensitive search

Filesystem Hierarchy Standard (FHS)

  • /boot – contains files related to the initial booting of the computer.
  • /bin – contains certain critical executable files, such as ls, cp, and mount.
  • /dev – contains device files like hard disks or CD-ROMs.
  • /sbin – similar to /bin, but it contains programs that are normally run only by the system administrator.
  • /etc – contains configuration files.
  • /home – user’s home directory.
  • /lib – contains program libraries.
  • /media – mount point for removable media.
  • /usr – contains the majority of user utilities and applications.
  • /var – variable files such as logs.
  • /tmp – contains temporary files.

Common environment variables

  • USER – your current username.
  • SHELL – the path to the current command shell (for example, /bin/bash).
  • PWD – the current working directory.
  • HOSTNAME – the hostname of the computer.
  • HOME – your home directory.
  • MAIL – the location of the user’s mail spool. Usually /var/spool/mail/USER.
  • LANG – your current language.
  • TZ – your time zone.
  • PS1 – the default prompt in bash.
  • TERM – the current terminal type (for example, xterm).
  • DISPLAY – the display used by X. This variable is usually set to :0.0, which means the first display on the current computer.
  • HISTFILESIZE – the maximum number of lines contained in the history file.
  • EDITOR – the user’s preferred text editor.
  • PATH - holds the colon-separated list of directories used to find commands that you enter
  • MANPATH – the list of directories to search for manual pages.
  • OSTYPE – the type of operating system.cd

Inode

An inode is a data structure that stores various information about a file in Linux, such as the access mode (read, write, execute permissions), ownership, file type, file size, group, number of links, etc. Each inode is identified by an integer number. An inode is assigned to a file when it is created. Calls ls -il

Links

Links in Linux are used to refer to a single file by multiple names. They are used to make files more accessible, to give commands multiple names, to enable programs that look for the same files in different locations to access the same files, etc. Links in Linux have a similar purpose as shortcuts in Windows.

  • hard links - duplicate directory entry. Both directory entries point to the same file. Hard links can only be created to files on the same partition. ln [original filename link filename]
  • soft(symbolic links) - special type of file that points to other files, instead of pointing to data on the hard drive. Can work over different partitions. ln -s [original filename link filename]

Wildcard

Is a symbol or a set of symbols that stands in for other characters. It can be used to substitute for any other character or characters in a string.

  • ? – matches a single character. For example, O??d matches anything that begins with O, ends with d and has two characters in between (like Oind, Okhd, Oerd, but not Oereed, Oad, Oerererd.)
  • * – matches any character or set of characters, including no character. For example, O*d matches anything that begins with O and ends with d (like Oind, Okhd, Oerd, Oereed, Oad, Oerererd, Od, Oarmeerrd).
  • Bracketed values – match characters enclosed in square brackets. For example, O[ac]d matches only Oad and Ocd. You can also specify a range of values: O[a-e]d matches Oad, Obd, Ocd, Odd and Oed.

Streams

Linux shells use three standard streams:

  • standard input (stdin) – usually the input from the keyboard. For example, commands that are executed by typing them.
  • standard output (stdout) – displays the output from commands, usually to the terminal.
  • standard error(stderr) – displays error output from commands. It is usually sent to the same output as standard output, but it can be redirected.

Redirect input and output

Here is a list of all redirection symbols: stream redirections symbols

Pipe data between programs

In Linux, you can make one command’s output the standard input of another command. This process is called piping and it is done using the pipe symbol (|). Piping lets you have one command work on some data and then have the next command deal with the results. For ex: ps -A | tail -n 15 | >> some_text_file.txt

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment