Playing with command line

Playing with command line

Why should we learn about command-line?

Fluency in using the Linux command-line interface is a skill often neglected or considered arcane, but it improves your flexibility and productivity as an engineer in both obvious and subtle ways. This is a selection of notes and tips on using the Linux command-line interface that we've found useful when working on Linux. Some tips are elementary, and some are fairly specific, sophisticated, or obscure. This page is not long, but if you can use and recall all the items here perfectly then you know a lot!

Who is this guide for?

This guide contains information that is both useful for beginners and experienced users. The goals of this translation are to make the content broad in scope, give concrete examples about the most common use case, and be concise by providing only the information that is essential and relevant to the reader. Everything contained in this article is incredibly important.

It includes both "standard" Unix commands as well as ones that require special package installs -- so long as they are important enough to merit inclusion.

Note

Use Explainshell to get a helpful breakdown of what commands, options, pipes etc. do.

Let's start with basics

  • Basic Bash

Actually, type man bash and at least skim the whole thing; it's pretty easy to follow and not that long. Alternate shells can be nice, but Bash is powerful and always available (learning only zsh, fish, etc., while tempting on your own laptop, restricts you in many situations, such as using existing servers)

  • Text-based editor

You can start with nano which is the simplest tool for basic editing (opening, editing, saving, searching).

However, for the power user, there is no substitute for Vim (vi), it is hard-to-learn but venerable, fast, and full-featured editor.

  • Finding documentation

Know how to read official documentation with man (for the inquisitive, man man lists the section numbers, e.g. 1 is "regular" commands, 5 is files/conventions, and 8 are for administration).

Know that some commands are not executables, but Bash builtins, and that you can get help on them with help and help -d. You can find out whether a command is an executable, shell builtin or an alias by using type command.

curl cheat.sh/command will give a brief "cheat sheet" with common examples of how to use a shell command.

  • Learn about redirection of output and input using> and < and pipes using |. Know > overwrites the output file and >> appends. Learn about stdout and stderr.
  • Be familiar with Bash job management:&, ctrl-z, ctrl-c, jobs, fg, bg,kill, etc.

  • Know ssh, and the basics of passwordless authentication, via ssh-agent, ssh-add, etc.

  • Basic file management: ls and ls -l (in particular, learn what every column in ls -l means), less,head,tail and tail -f(or even better,less +F),ln and ln -s (learn the differences and advantages of hard versus soft links), chown , chmod , du (for a quick summary of disk usage: du -hs *) . For filesystem management,df, mount , fdisk , mkfs , lsblk . Learn what an inode is (ls -i or df -i).

  • Basic network management: ip or ifconfig, dig , traceroute , route.

  • Learn and use a version control management system, such as git.

  • Know regular expressions well, and the various flags to grep/egrep . The -i , -o, -v, -A, -B, and -C options are worth knowing.

  • Learn to use apt-get, yum, dnf or pacman (depending on distro) to find and install packages. And make sure you have pip to install Python-based command-line tools (a few below are easiest to install via pip).

Basic commands you should know

  • pwd - Use the pwd command to find out the path of the current working directory (folder) you’re in.

  • cd - To navigate through the Linux files and directories, use the cd command.

  • ls - The ls command is used to view the contents of a directory.

  • cat - cat (short for concatenate) s used to list the contents of a file on the standard output (sdout).

  • cp - Use the cp command to copy files from the current directory to a different directory

  • mv - The primary use of the mv command is to move files, although it can also be used to rename files.

  • mkdir - Use mkdir command to make a new directory — if you type mkdir K8s it will create a directory called K8s.

  • rmdir - If you need to delete a directory, use the rmdir command.

  • rm - The rm command is used to delete directories and the contents within them.

  • touch - The touch command allows you to create a blank new file through the Linux command line.

  • locate - You can use this command to locate a file, just like the search command in Windows.

  • grep - Grep lets you search through all the text in a given file.

  • sudo - Short for “SuperUser Do”, this command enables you to perform tasks that require administrative or root permissions.

  • df - Use df command to get a report on the system’s disk space usage, shown in percentage and KBs.

  • head - The head command is used to view the first lines of any text file.

  • tail - This one has a similar function to the head command, but instead of showing the first lines, the tail command will display the last ten lines of a text file.

  • tar - The tar command is the most used command to archive multiple files into a tarball — a common Linux file format that is similar to zip format, with compression being optional.

  • chmod - chmod is another Linux command, used to change the read, write, and execute permissions of files and directories.

  • chown - In Linux, all files are owned by a specific user. The chown command enables you to change or transfer the ownership of a file to the specified username.

  • jobs - jobs command will display all current jobs along with their statuses. A job is basically a process that is started by the shell.

  • kill - If you have an unresponsive program, you can terminate it manually by using the kill command. It will send a certain signal to the misbehaving app and instructs the app to terminate itself.

  • ping - Use the ping command to check your connectivity status to a server.

  • man - Confused about the function of certain Linux commands? Don’t worry, you can easily learn how to use them right from Linux’s shell by using the man command.

  • echo - This command is used to move some data into a file.

  • zip,unzip - Use the zip command to compress your files into a zip archive, and use the unzip command to extract the zipped files from a zip archive.

  • useradd - ince Linux is a multi-user system, this means more than one person can interact with the same system at the same time.