====== Linux Command Line Interface ====== My friend asked me about some tutorial or "cheat sheet" on Linux command line interface. A goal is to explain basics for people who came to Linux from other, mostly GUI oriented operating systems. When you log into remote server via ssh, or invoke a terminal app on local Linux machine, you get a command line prompt, like this: $ _ It's a prompt from the command line shell. To quit from the current shell, use command: exit ====== Shell versions ====== When troubleshooting, it's important to know which exacly shell you are using. There are a few different shell applications in Linux. On most Linuxes and on Mac, the default interactive shell is Bash (/bin/bash). On Ubuntu the default shell /bin/sh is Dash (/bin/dash), for efficiency reasons. To quickly check which version you are logged in, use command "ps $$". It replaces $$ with a process ID of the current shell, and shows the details. In case of Bash, you will see "-bash" as the application name: $ ps $$ PID TTY STAT TIME COMMAND 3011 pts/0 Ss 0:00 -bash In case of Dash, you will see /bin/sh: $ ps $$ PID TTY STAT TIME COMMAND 3011 pts/0 Ss 0:00 /bin/sh Other shells are also available, like tcsh, ksh, zsh and others. But most people use bash, so I will assume it from now on. ====== My Choice: Bash Commander ====== As for my taste, I prefer shells with "visual" navigation, in a style of famous Norton Commander. My personal choice is Bash Commander. It's essentially the same Bash, extended with a capability to display two windows and navigate through filesystem with cursor keys. Bash Commander can be installed from Github: [[https://github.com/sergev/bash-commander]] ====== Environment Variables ====== When Bash starts, it reads the environment variables from file ~/.bashrc. Here ~ means your home directory. Usually .bashrc file already exists for you, pre-installed by your system admin. You can explore it by using "less" utility: $ cd ~ $ less .bashrc The less utility allows to browse a text file and scroll it forward and backward with PageDown/PageUp keys and down/up arrows. Note that .bashrc is used only by interactive non-login shells. For login shells other files are used, like /etc/profile, ~/.bash_profile, ~/.bash_login, and ~/.profile. If you cannot find where some environment variable is configured in your .bashrc, look at the above files. For all details of Bash configuration, see the manual page: man bash The manual pages are available for all other CLI commands, for example: man less or man man To see all the envirinment variables, use: env The most important variable is PATH. It's a list of directories to search for commands, separated by colon. To show a value of a particular variable, use echo: echo $PATH To change value of (global) variable, use: export NAME=value The assignment "=" sign is important here, and there must be no spaces around it. For example: export EDITOR=/usr/bin/nano By convention, the EDITOR variable is used by many Linux commands when they need to invoke a text editor for you to modify some file. By default, "vi" editor is used, and it causes a lot of trouble for most inexperienced users. So I recommend to set EDITOR to something more friendly, like the Nano editor. Nano is typically pre-installed on most Linuxes, and it has pretty intuitive interface. For example, to exit Nano, ^X is used. ====== Prompt ====== The default Bash prompt is boring. It can be easily enhanced by modifying the PS1 variable. Here is the value I use on all my Linux accounts: PS1="\[\033[44m\](\h) \W\[\033[m\033[1m\] \\\$\[\033[m\] " This looks frightening, but fear not! Most of ugly stuff there is about colors. Two fields are important: \h means the host name, and \W is the basename of the current directory. Just put this line into your ~/.bashrc file and re-load it: $ cd ~ $ source .bashrc It's easier to explain on example. Say when I log into my server sergev.org and go to directory /website/pages, the prompt becomes like this: (sergev) pages $ So from the prompt I can see on which server and at which directory I am right now. ====== History ====== In Bash, every command entered by user is stored to the History buffer. This buffer has a large size, typically 1000 lines, and is preserved across login sessions. To browse the list, use the history command, for example: $ history 210 grep export .bashrc 211 ls -l /usr/bin/* 212 man nano 213 grep alias .bashrc 214 man netstat 215 traceroute somewhere.net Any line from the history can be repeated by number prefixed with ! symbol. Say, let's repeat the "man nano" command: !212 Previous command can be repeated as: !! Also you can use the Up and Down arrows to browse through the History buffer. Any previous command line can be edited and run again. Parameters from the previous command also are available via the ! prefix. For example, !$ means the $ traceroute somewhere.net traceroute to somewhere.net (3.33.152.147), 30 hops max, 60 byte packets ... $ ping !$ ping somewhere.net ... To search for command in the History buffer, use ^R. For example, let's search for the previous man command. Type ^R and enter "man". The prompt becomes: (reverse-i-search)`man': man tail Press Enter to execute, or ^R to continue searching, or ^C to cancel. ====== Aliases ====== Bash allows to define short names for commonly used commands. Here is a list of shortcuts I define in my ~/.bashrc script: alias h=history alias _='stty sane' alias ls='ls --color=auto' alias grep='grep --color=auto' alias df='df -m -x squashfs -x tmpfs -x devtmpfs' Instead of typing "history" every time, I prefer to shorten it as just "h". Command "_" helps to quickly restore the terminal modes in case it's corrupted by some previous command. Commands "ls" and "grep" can colorize the output. I redefine them as aliases to enable colors by default. For "df" command I disable temporary filesystems, so the output includes only real disks. To browse your aliases, use: alias ====== Cheat Sheet ====== Here is a list of frequently used Linux commands, with brief comments. ==== Navigating Files and Directories ==== cd dir Enter the specified directory. ls List files. pwd Print the name of the current (working) directory. The same name is available as the environment variable PWD. It can be displayed with the echo command: echo $PWD ==== Browsing Files ==== cat file.txt Display file contents to the standard output. It works fine for short files. For longer files better use the less command: less file.txt It displays one page of the text file. You can navigate via the contents with PageDown/PageUp keys or Down/Up arrows. head file.txt Show the first 10 lines of the file. tail Show the last 10 lines of the file. grep pattern file.txt Search text files for a string (or pattern). diff old.txt new.txt Compare two files and highlight the differences between them. find . -name '*foo*' Search current directory tree for a file with given name. ==== Exploring the System ==== ps auxww Display information about the active processes. top Display active processes and kernel info in interactive mode. Type 'i' to hide idle processes. Use 'q' to stop. df Report file system disk space usage. free Display amount of free and used memory in the Linux system. finger Display information about the system users. groups Print the groups a user is in. It is important to know your groups, as your access to the system services depends on it. uname --all With --all option, print the following system information: * kernel name * network node hostname * kernel release * kernel version * machine hardware name * processor type * hardware platform * operating system w Show who is logged on and what they are doing. whoami Print the user name associated with the current effective user ID. sudo command Execute any command as the superuser. To be able to do it, the administrator should include you into the list of 'sudoers'. For example: sudo lshw | less Extract detailed information on the hardware configuration of the machine. ==== Modifying the Files ==== chmod +w file.txt Change file access mode. chown vak /home/vak Change file owner. mkdir /mnt/media Create a directory. mv old new Move (rename) files. cp -a file new-copy Copy files. With -a option, the whole file tree is copied recursively, with all attributes preserved (including modification time). kill 123 Kill the given process ID by sending a signal to it. killall nano Kill a process by name. passwd Change user password. ====== Networking ====== ip addr Display information about your IP address. netstat -lt Show network information: options -lt mean to see the listening TCP/IP sockets. The netstat utility can do much more. Visit [[https://linuxhint.com/install-netstat-command-linux/]] for details. ping somewhere.net Send network packets to the remote device to make sure it is alive. traceroute somewhere.net Send network packets to the remote device and count the hops from router to router as packets make their way via the network. curl -O https://www.keycdn.com/img/example.jpg Download a file from remote server. ssh somewhere.net Log into remote device. ====== References ====== For more info about Linux commands, use the links: Files: * [[https://www.howtogeek.com/248780/how-to-compress-and-extract-files-using-the-tar-command-on-linux/ | tar]] * [[https://www.howtogeek.com/428654/how-to-monitor-the-progress-of-linux-commands-with-pv-and-progress/ | pv]] * [[https://www.howtogeek.com/278599/how-to-combine-text-files-using-the-cat-command-in-linux/ | cat]] * [[https://www.howtogeek.com/424234/how-to-use-the-linux-cat-and-tac-commands/ | tac]] * [[https://www.howtogeek.com/437958/how-to-use-the-chmod-command-on-linux/ | chmod]] * [[https://www.howtogeek.com/496056/how-to-use-the-grep-command-on-linux/ | grep]] * [[https://www.howtogeek.com/410532/how-to-compare-two-text-files-in-the-linux-terminal/ | diff]] * [[https://www.howtogeek.com/666395/how-to-use-the-sed-command-on-linux/ | sed]] * [[https://www.howtogeek.com/427086/how-to-use-linuxs-ar-command-to-create-static-libraries/ | ar]] * [[https://www.howtogeek.com/663440/how-to-use-linuxs-man-command-hidden-secrets-and-basics/ | man]] * [[https://www.howtogeek.com/659146/how-to-use-pushd-and-popd-on-linux/ | pushd]] * [[https://www.howtogeek.com/659146/how-to-use-pushd-and-popd-on-linux/ | popd]] * [[https://www.howtogeek.com/745921/how-to-use-the-fsck-command-on-linux/ | fsck]] * [[https://www.howtogeek.com/700310/how-to-recover-deleted-files-on-linux-with-testdisk/ | testdisk]] * [[https://www.howtogeek.com/693549/how-to-use-the-seq-command-on-linux/ | seq]] * [[https://www.howtogeek.com/682244/how-to-use-the-fd-command-on-linux/ | fd]] * [[https://www.howtogeek.com/678022/how-to-use-pandoc-to-convert-files-on-the-linux-command-line/ | pandoc]] * [[https://www.howtogeek.com/666127/how-to-use-the-cd-command-on-linux/ | cd]] * [[https://www.howtogeek.com/658904/how-to-add-a-directory-to-your-path-in-linux/ | $PATH]] * [[https://www.howtogeek.com/562941/how-to-use-the-awk-command-on-linux/ | awk]] * [[https://www.howtogeek.com/542677/how-to-use-the-join-command-on-linux/ | join]] * [[https://www.howtogeek.com/529219/how-to-parse-json-files-on-the-linux-command-line-with-jq/ | jq]] * [[https://www.howtogeek.com/538778/how-to-use-the-fold-command-on-linux/ | fold]] * [[https://www.howtogeek.com/533406/how-to-use-the-uniq-command-on-linux/ | uniq]] * [[https://www.howtogeek.com/499623/how-to-use-journalctl-to-read-linux-system-logs/ | journalctl]] * [[https://www.howtogeek.com/481766/how-to-use-the-tail-command-on-linux/ | tail]] * [[https://www.howtogeek.com/451022/how-to-use-the-stat-command-on-linux/ | stat]] * [[https://www.howtogeek.com/448446/how-to-use-the-ls-command-on-linux/ | ls]] * [[https://www.howtogeek.com/444814/how-to-write-an-fstab-file-on-linux/ | fstab]] * [[https://www.howtogeek.com/446071/how-to-use-the-echo-command-on-linux/ | echo]] * [[https://www.howtogeek.com/444233/how-to-use-the-less-command-on-linux/ | less]] * [[https://www.howtogeek.com/439500/how-to-use-the-chgrp-command-on-linux/ | chgrp]] * [[https://www.howtogeek.com/438435/how-to-use-the-chown-command-on-linux/ | chown]] * [[https://www.howtogeek.com/434180/how-to-use-the-rev-command-on-linux/ | rev]] * [[https://www.howtogeek.com/428742/how-to-use-the-look-command-on-linux/ | look]] * [[https://www.howtogeek.com/427805/how-to-use-the-strings-command-on-linux/ | strings]] * [[https://www.howtogeek.com/426014/how-to-use-the-linux-type-command/ | type]] * [[https://www.howtogeek.com/423214/how-to-use-the-rename-command-on-linux/ | rename]] * [[https://www.howtogeek.com/414082/how-to-zip-or-unzip-files-from-the-linux-terminal/ | zip]] * [[https://www.howtogeek.com/414082/how-to-zip-or-unzip-files-from-the-linux-terminal/ | unzip]] * [[https://www.howtogeek.com/414082/how-to-zip-or-unzip-files-from-the-linux-terminal/ | mount]] * [[https://www.howtogeek.com/414082/how-to-zip-or-unzip-files-from-the-linux-terminal/ | umount]] * [[https://www.howtogeek.com/411366/how-to-copy-files-with-the-install-command-on-linux/ | install]] * [[https://www.howtogeek.com/106873/how-to-use-fdisk-to-manage-partitions-on-linux/ | fdisk]] * [[https://www.howtogeek.com/443342/how-to-use-the-mkfs-command-on-linux/ | mkfs]] * [[https://www.howtogeek.com/409115/how-to-delete-files-and-directories-in-the-linux-terminal/ | rm]] * [[https://www.howtogeek.com/409115/how-to-delete-files-and-directories-in-the-linux-terminal/ | rmdir]] * [[https://www.howtogeek.com/427480/how-to-back-up-your-linux-system/ | rsync]] * [[https://www.howtogeek.com/409611/how-to-view-free-disk-space-and-disk-usage-from-the-linux-terminal/ | df]] * [[https://www.howtogeek.com/427982/how-to-encrypt-and-decrypt-files-with-gpg-on-linux/ | gpg]] * [[https://www.howtogeek.com/102468/a-beginners-guide-to-editing-text-files-with-vi/ | vi]] * [[https://www.howtogeek.com/howto/42980/the-beginners-guide-to-nano-the-linux-command-line-text-editor/ | nano]] * [[https://www.howtogeek.com/275069/how-to-create-multiple-subdirectories-with-one-linux-command/ | mkdir]] * [[https://www.howtogeek.com/450366/how-to-get-the-size-of-a-file-or-directory-in-linux/ | du]] * [[https://www.howtogeek.com/287014/how-to-create-and-use-symbolic-links-aka-symlinks-on-linux/ | ln]] * [[https://www.howtogeek.com/415442/how-to-apply-a-patch-to-a-file-and-create-patches-in-linux/ | patch]] * [[https://www.howtogeek.com/109369/how-to-quickly-resize-convert-modify-images-from-the-linux-terminal/ | convert]] * [[https://www.howtogeek.com/451262/how-to-use-rclone-to-back-up-to-google-drive-on-linux/ | rclone]] * [[https://www.howtogeek.com/425232/how-to-securely-delete-files-on-linux/ | shred]] * [[https://www.howtogeek.com/425232/how-to-securely-delete-files-on-linux/ | srm]] Processes: * [[https://www.howtogeek.com/439736/how-to-create-aliases-and-shell-functions-on-linux/ | alias]] * [[https://www.howtogeek.com/662422/how-to-use-linuxs-screen-command/ | screen]] * [[https://www.howtogeek.com/668986/how-to-use-the-linux-top-command-and-understand-its-output/ | top]] * [[https://www.howtogeek.com/411979/how-to-set-process-priorities-with-the-nice-and-renice-commands-in-linux/ | nice]] * [[https://www.howtogeek.com/411979/how-to-set-process-priorities-with-the-nice-and-renice-commands-in-linux/ | renice]] * [[https://www.howtogeek.com/428654/how-to-monitor-the-progress-of-linux-commands-with-pv-and-progress/ | progress]] * [[https://www.howtogeek.com/732736/how-to-use-strace-to-monitor-linux-system-calls/ | strace]] * [[https://www.howtogeek.com/687970/how-to-run-a-linux-program-at-startup-with-systemd/ | systemd]] * [[https://www.howtogeek.com/671422/how-to-use-tmux-on-linux-and-why-its-better-than-screen/ | tmux]] * [[https://www.howtogeek.com/669835/how-to-change-your-default-shell-on-linux-with-chsh/ | chsh]] * [[https://www.howtogeek.com/465243/how-to-use-the-history-command-on-linux/ | history]] * [[https://www.howtogeek.com/451386/how-to-use-at-and-batch-on-linux-to-launch-processes/ | at]] * [[https://www.howtogeek.com/451386/how-to-use-at-and-batch-on-linux-to-launch-processes/ | batch]] * [[https://www.howtogeek.com/456943/how-to-use-the-free-command-on-linux/ | free]] * [[https://www.howtogeek.com/450894/how-to-use-the-which-command-on-linux/ | which]] * [[https://www.howtogeek.com/449335/how-to-use-the-dmesg-command-on-linux/ | dmesg]] * [[https://www.howtogeek.com/449160/how-to-change-user-data-with-chfn-and-usermod-on-linux/ | chfn]] * [[https://www.howtogeek.com/449160/how-to-change-user-data-with-chfn-and-usermod-on-linux/ | usermod]] * [[https://www.howtogeek.com/448271/how-to-use-the-ps-command-to-monitor-linux-processes/ | ps]] * [[https://www.howtogeek.com/441534/how-to-use-the-chroot-command-on-linux/ | chroot]] * [[https://www.howtogeek.com/435164/how-to-use-the-xargs-command-on-linux/ | xargs]] * [[https://www.howtogeek.com/428174/what-is-a-tty-on-linux-and-how-to-use-the-tty-command/ | tty]] * [[https://www.howtogeek.com/427004/how-to-use-the-pinky-command-on-linux/ | pinky]] * [[https://www.howtogeek.com/426031/how-to-use-the-linux-lsof-command/ | lsof]] * [[https://www.howtogeek.com/424334/how-to-use-the-vmstat-command-on-linux/ | vmstat]] * [[https://www.howtogeek.com/423286/how-to-use-the-timeout-command-on-linux/ | timeout]] * [[https://www.howtogeek.com/415914/how-to-use-the-wall-command-on-linux/ | wall]] * [[https://www.howtogeek.com/415535/how-to-use-the-yes-command-on-linux/ | yes]] * [[https://www.howtogeek.com/413213/how-to-kill-processes-from-the-linux-terminal/ | kill]] * [[https://www.howtogeek.com/410299/how-to-pause-a-bash-script-with-the-linux-sleep-command/ | sleep]] * [[https://www.howtogeek.com/111479/htg-explains-whats-the-difference-between-sudo-su/ | sudo]] * [[https://www.howtogeek.com/111479/htg-explains-whats-the-difference-between-sudo-su/ | su]] * [[https://www.howtogeek.com/415977/how-to-use-the-time-command-on-linux/ | time]] * [[https://www.howtogeek.com/50787/add-a-user-to-a-group-or-second-group-on-linux/ | groupadd]] * [[https://www.howtogeek.com/50787/add-a-user-to-a-group-or-second-group-on-linux/ | usermod]] * [[https://www.howtogeek.com/howto/ubuntu/see-which-groups-your-linux-user-belongs-to/ | groups]] * [[https://www.howtogeek.com/508993/how-to-check-which-gpu-is-installed-on-linux/ | lshw]] * [[https://www.howtogeek.com/411925/how-to-reboot-or-shut-down-linux-using-the-command-line/ | shutdown]] * [[https://www.howtogeek.com/411925/how-to-reboot-or-shut-down-linux-using-the-command-line/ | reboot]] * [[https://www.howtogeek.com/411925/how-to-reboot-or-shut-down-linux-using-the-command-line/ | halt]] * [[https://www.howtogeek.com/411925/how-to-reboot-or-shut-down-linux-using-the-command-line/ | poweroff]] * [[https://www.howtogeek.com/447443/how-to-change-account-passwords-on-linux/ | passwd]] * [[https://www.howtogeek.com/198615/how-to-check-if-your-linux-system-is-32-bit-or-64-bit/ | lscpu]] * [[https://www.howtogeek.com/101288/how-to-schedule-tasks-on-linux-an-introduction-to-crontab-files/ | crontab]] * [[https://www.howtogeek.com/410442/how-to-display-the-date-and-time-in-the-linux-terminal-and-use-it-in-bash-scripts/ | date]] * [[https://www.howtogeek.com/440848/how-to-run-and-control-background-processes-on-linux/ | bg]] * [[https://www.howtogeek.com/440848/how-to-run-and-control-background-processes-on-linux/ | fg]] Networking: * [[https://www.howtogeek.com/513003/how-to-use-netstat-on-linux/ | netstat]] * [[https://www.howtogeek.com/355664/how-to-use-ping-to-test-your-network/ | ping]] * [[https://www.howtogeek.com/657780/how-to-use-the-traceroute-command-on-linux/ | traceroute]] * [[https://www.howtogeek.com/657911/how-to-use-the-ip-command-on-linux/ | ip]] * [[https://www.howtogeek.com/681468/how-to-use-the-ss-command-on-linux/ | ss]] * [[https://www.howtogeek.com/680086/how-to-use-the-whois-command-on-linux/ | whois]] * [[https://www.howtogeek.com/675010/how-to-secure-your-linux-computer-with-fail2ban/ | fail2ban]] * [[https://www.howtogeek.com/664589/how-to-use-bmon-to-monitor-network-bandwidth-on-linux/ | bmon]] * [[https://www.howtogeek.com/663056/how-to-use-the-dig-command-on-linux/ | dig]] * [[https://www.howtogeek.com/440391/how-to-use-the-finger-command-on-linux/ | finger]] * [[https://www.howtogeek.com/423709/how-to-see-all-devices-on-your-network-with-nmap-on-linux/ | nmap]] * [[https://www.howtogeek.com/412626/how-to-use-the-ftp-command-on-linux/ | ftp]] * [[https://www.howtogeek.com/447033/how-to-use-curl-to-download-files-from-the-linux-command-line/ | curl]] * [[https://www.howtogeek.com/281663/how-to-use-wget-the-ultimate-command-line-downloading-tool/ | wget]] * [[https://www.howtogeek.com/410423/how-to-determine-the-current-user-account-in-linux/ | who]] * [[https://www.howtogeek.com/410423/how-to-determine-the-current-user-account-in-linux/ | whoami]] * [[https://www.howtogeek.com/410423/how-to-determine-the-current-user-account-in-linux/ | w]] * [[https://www.howtogeek.com/177621/the-beginners-guide-to-iptables-the-linux-firewall/ | iptables]] * [[https://www.howtogeek.com/424510/how-to-create-and-install-ssh-keys-from-the-linux-shell/ | ssh-keygen]] * [[https://www.howtogeek.com/115116/how-to-configure-ubuntus-built-in-firewall/ | ufw]]