Master the basic shell commands

ยท 700 words ยท 4 minute read

Today we’ll do a warm up session! We’re going to talk about the basic shell commands that you’ll get to use in your programming journey.

What is the shell and why should I use it? ๐Ÿ”—

Let’s start with a few basics about the shell itself. The shell is a program that uses a command line interface, lines of texts, sometimes referred as CLI, instead of a graphical user interface, icons, sometimes referred as GUI, to interact with the Operating System.

It might seem cumbersome at the beginning but with just a bit of practice, using the shell is faster than using the GUI for 99% of your interactions with your computer.

What is the difference between shell and bash? ๐Ÿ”—

The shell is a specification. The bash is one of its most widely used implementation. Many others exist.

What are the commands I need to master? ๐Ÿ”—

Glad you ask. Letโ€™s start with a dozen of commands, which I think are the most useful ones. They will give you a quick overview of the power of the shell.

man ๐Ÿ”—

Description: format and display the on-line manual pages
Usage: man $command_name # we use $var but man pages underline required variables

You can try that command when you have a doubt about a shell command. Try man ls and you’ll see the ls description page and discover all the ls options. Type q to get back to the command prompt.

pwd ๐Ÿ”—

Description: return working directory name
Usage: pwd

Gives you the absolute path to the current working directory. In simple terms, it tells you where you are in your file system.

ls ๐Ÿ”—

Description: list directory contents
Usage: ls [file] # optional parameters are enclosed in squared brackets
Main options: -1 (number one), -l (lowercase "ell")

Certainly among the top 3 commands you will use. If you donโ€™t specify any arguments or any option, it displays the list of all the files in the current directory. You can specify a path to get the content of a specific directory. Use ls -1 to get one entry by line for the output and ls -l to get the list in long format (the file permisions are added for instance).

cd ๐Ÿ”—

Description: change current directory
Usage: cd [path]

If you donโ€™t specify anything you will get back to your home directory. You can use a relative path (a path relative to your current position) or an absolute path and go to that particular folder. .. (two dots) means one level up and you can go multiple levels up using a slash every two dots.

mv ๐Ÿ”—

Description: move (rename) files
Usage: mv $source_file $target_file and mv $source_file $target_directory

This is a versatile command. The two main usages are:

  1. with a source file and a target file in the same directory, mv renames the source as the target
  2. with a source filename and a target directory, mv moves the file source to the target directory

cp ๐Ÿ”—

Description: copy file(s)
Usage: cp $source_file $target_file and cp $source_file $target_directory

The cp command can be used to copy a file in the same directory or to copy it from the current directory to the directory specified by a relative or absolute path.

mkdir ๐Ÿ”—

Description: make directories
Usage: mkdir $directory_name

If a simple directory name is specified, a directory is created in the current directory. A path finishing with a new directory name can also be used to created a directory at a specific place.

rm ๐Ÿ”—

Description: remove files or directories
Usage: rm file
Main options: -r (recursively removes the files from the root down to the leaves), -f (no prompting for confirmation)

โ€œWith great power comes great responsibilityโ€ (Uncle Ben – Spiderman)

rm allows you to remove (aka delete) a file. If you specify the -r option, you can remove a file hierarchy, i.e. a directory. With the -f option, you donโ€™t get a second chance: the argument is removed without prompting for confirmation.

Conclusion ๐Ÿ”—

We’ve discussed just the basic shell commands. Feel free to explore the man pages or google for specific usage and I am pretty sure that you’ll find a command that fits your needs. Have fun! :)

References ๐Ÿ”—