In 2023, you won’t need to open a terminal window to use your Linux computer. Still, the command line interface is a useful tool to use.
Edited by Rob Coenraads
Any text you type into a terminal window must be interpreted by the system. That role is reserved for the shell. Since 1989, the default shell on almost all Unix systems has been the Bourne Again Shellbetter known as Bash. However, there are alternatives such as Zsh who are gaining ground. The commands we discuss here work in both shells.
STEP 1 Navigate
If you use the command line interface (CLI), it is useful to know where you are in the system. With the command
pwd (Print working directory) you will immediately see that information. To navigate to a different directory, you can use the change directory command followed by the location you want to go to.
cd /Afbeeldingen for example, sends you to your images folder. CD with nothing after it sends you straight to your home folder. In combination with two dots sends CD .. you to the parent folder.
Do you want to know which files are in a folder? Then use the list command ls. Here you can add options to also show hidden files with -a or get more detailed information with -l. Are you shown a really long list? Then you can filter with grep. Are you looking for pictures of your trip by the sea and do you see “sea” in the file name? By means of ls -al | grab sea you will get an overview of this.
You can navigate through the system with just a few commands
STEP 2 Edit folders and files
If you are performing file operations via the CLI for the first time, it is best to navigate to /tmp first. This way you cannot damage the system. Once in /tmp, you can test file operations. So you can with it mkdircommand create a folder and with it touchcommand you can then create a test file again. Files on Linux do not have to have extensions, but that makes it clearer for you. Create two test folders and put two test files in one of the folders; it is best not to use spaces in file and folder names.
With the move command pl you can move files from one folder to another. The syntax for this is not very complicated: mv file target location. You can also use the move command to rename files. At the target location, enter the new file name. To copy a file, use the copy command cp. The syntax is the same as for move – the target location can be a folder or a new file name. These locations are relative and continue on the directory you are working in unless you specify the absolution location (e.g. /tmp/Testmap-1/test-1.txt).
It is interesting to give the files some content right away. You can do this in the command line by using comparators. With ‘>’ you can replace the contents of a file with the output of a command. Don’t delete everything? Then use ‘>>’. The output is then appended to the file. With the echo command you can generate that output: echo “this is a first test file” > test-1.txt.
STEP 3 View files
If you want to know what’s in files and folders, you have a number of options.
The most obvious options are less and more. The first command displays the file’s contents in the terminal window until there’s no more space – pressing the spacebar takes you to the next ‘page’. In less is very much possible. To see the many options in less, press the ‘h’ key. Close the reader by pressing ‘q’. More is an earlier and less extensive version of the same reader.
You can also use the concatenation command cat which is especially useful if you want to use the file contents in other commands, since the cat output is plain text that can serve as input to other commands.
You just need a few commands
STEP 4 Clean up
To remove files or folders, use the remove command
rm. This command works like this on files, for empty directories use -d- behind it and for filled directories use the recursive function with rm -r. This means you delete a folder and its contents and pay close attention to this: the files do not end up in a trash can and cannot be retrieved after you delete them.
STEP 5 Wildcards
Until now we only moved 1 file at a time. The power of the CLI, however, lies in the fact that you can also make edits in bulk. That’s what you use wild cards – asterisks that can mean anything and make the CLI more efficient than any GUI. For example, you can move all .jpg files to a folder at once; you can do this for all files, filenames and folders. In /tmp to delete the two test directories and content you can e.g
rm -r Testmap* enter.
Wildcards allow you to quickly make multiple edits at once
STEP 6 Read the manual
There are of course many more commands and options, for more info open a manual with
man rm so see the manual of the rm command. If no man page is available, try running the command with the -h- or –help-flag. Usually you get enough tools to understand the commands.