Commanding the Command Line

Commanding the Command Line

Tackling the Terminal, or shackling Shell.

Matt Graham's photo
Matt Graham
·Oct 24, 2021·

6 min read

It is a great advantage to understand the basics of the "shell", also known as "command Line" or "terminal". This tool allows the ability to accomplish and automate tasks on a computer without the use of a graphical user interface. It also allows sending of simple text commands to the computer to do things such as navigate through a directory or copy a file, and form the basis for many more complex automation and programming skills.

Different Operating Systems use different Shell software. MacOS uses terminal by default, with Windows using Command Prompt but also has Windows Terminal which was released in 2019. Linux uses have several different versions of the terminal depending on the distribution such as gnome-terminal, konsole, xterm, rxvt, kvt, nxterm, and eterm.

When jumping into the terminal there is various commands that allow you to navigate, create, remove, rename and many more. Lets run through some of the basic commands used.

pwd - returns the name of the directory currently being browsed

pwd
/Users/directory

List

ls [OPTIONS] [FILES / DIRECTORY] - lists files and directories within the file system, and shows detailed information about them.

ls
Desktop      Downloads    Movies    Pictures
Documents    Library      Music     Public

A couple of options that can be used such as:

  • ls -l - list with long format - show permissions
  • ls -a - list all files including hidden file starting with '.'

There are various other options which can be used such as

  • ls -d - list directories - with ' */'
  • ls -la - list long format including hidden files
  • ls -lh - list long format with readable file size
  • ls -ls - list with long format with file size
  • ls -r - list in reverse order
  • ls -R - list recursively directory tree
  • ls -s - list file size
  • ls -S - sort by file size
  • ls -t - sort by time & date
  • ls -X - sort by extension name

Change Directory

cd changes the directory/folder of the terminal's shell. Hint Pressing the tab button will auto complete the directory name. Lets have a look further on how cd works.

  • cd - by itself will return back to the home directory.
  • cd ~ - also returns back to the home directory.
  • cd / - changes to the root directory.
  • cd .. - changes to the parent directory.
  • cd Documents - changes to sub directory Documents.
  • cd Documents/Books - changes to sub directory Documents/Books.
  • cd /home/user/Desktop - changes to directory with absolute path /home/user/Desktop.

Dealing with white spaces in directories:

  • cd My\ Images
  • cd "My Images"
  • cd 'My Images'

Touch

The touch command is the easiest way to create new, empty files. It is also used to change the timestamps (i.e. dates and times of the most recent access and modification) on existing files and directories.

touch's syntax is

touch [option] [file_name(s)...]

Touch creates new files for any file names that are provided as arguments (i.e., input data) if files with such names do not already exist. Touch can create any number of files simultaneously. For example, the following command would create three new, empty files named file1, file2 and file3:

touch file1 file2 file3

Make Directory

mkdir - creates directories / folders. This command can create multiple directories at once as well as set the permissions for the directories. It is important to note that the user executing this command must have enough permissions to create a directory in the parent directory, or he/she may receive a ‘permission denied’ error.

Syntax:

mkdir [options...] [directory...]

Move

mv - is used to move files and directories.

$ mv [options] [source...] [destination...]

Some examples of its use is as follows:

  • mv file.c file1.h /home/usr/files/ - moves file.c file1.h files to /home/usr/files/ directory.
  • mv *.c bak - moves all C files in current directory to sub directory bak.
  • mv bak/* . - move all files in sub directory bak to current directory.
  • mv main.c main.bak - renames file main.c to main.bak.

Copy

cp - copies files and directories.

cp [options] [source...] [destination...]

Options

  • cp -a - archive files.
  • cp -f - force copy by removing the destination file if needed.
  • cp -i - interactive - ask before overwrite.
  • cp -R - recursive copy (including hidden files).
  • cp -u - update copy when source is newer than destination.
  • cp -v - verbose - print informative messages.

cp command examples

  • cp main.c bak - copies single file main.c to destination directory bak.
  • cp main.c def.h /home/usr/rapid/ - copies 2 files main.c and def.h to destination absolute path directory /home/usr/rapid/
  • cp *.c bak - copies all C files in current directory to sub directory bak.
  • cp src /home/usr/rapid/ - copies directory src to absolute path directory /home/usr/rapid/.
  • cp -R dev bak - copies all files and directories in dev recursively to subdirectory bak.
  • cp -f test.c bak - force file copy.
  • cp -i test.c bak - interactive prompt before file overwrite.
  • cp -u * bak - update all files in current directory - copy only newer files to destination directory bak.

Remove

rm is used to remove objects such as files, and directories. By default, it does not remove directories. This command normally works silently and so be very careful while running rm command because once deleted, the files are not able to be recovered.

rm [options...] [files / directories ...]

rm command examples

  • rm a.txt - removes the file a.txt.
  • rm b.txt c.txt - removes the file b.txt and c.txt file.
  • rm -i d.txt - asks the user for confirmation before removing each file.
  • rm -f e.txt - This option overrides this minor protection and removes the file forcefully.
  • rm -r /files/* - Recursive Deletion: with -r(or -R) option rm command performs a tree-walk and will delete all the files and sub-directories recursively of the parent directory. At each stage it deletes everything it finds. Normally, rm wouldn’t delete the directories but when used with this option, it will delete.

CAT command

cat [options...] [files / directories ...]

cat - displays the content of text files and to combine several files to one file.

cat list1.txt - views a text file

cat list1.txt
milk
bread
apples

cat list2.txt
house
car

cat list1.txt list2.txt - Combines 2 text files

cat list1.txt list2.txt
milk
bread
apples

house
car

cat list1.txt list2.txt > todo.txt - Combines 2 text files to another file.

There is a tonne more that is possible within the Command line, however this should be sufficient to getting started.

 
Share this