Useful Intermediate Linux Commands
After you’ve gotten familiar with Linux and learned how to use some basic commands, you might be asking yourself where you can go from there. This article will cover a few more commands and programs that are often used when working with the Linux bash before introducing you to the concept of concatenating commands and redirecting their inputs and outputs which will allow you to work even more efficiently.
How to Copy Files Between Computers
scp stands for secure copy. As the name suggests, this program allows you to transfer files from a local machine to a remote computer and vice-versa. The following command copies a local file to a remote computer:
scp <local_file> <remote_user>@<remote_machine>:<remote_path>
Use the following syntax to transfer a file from a remote computer to your machine:
scp <remote_user>@<remote_machine>:<remote_path> <local_path>
This command is incredibly useful when working with developer boards like the Raspberry Pi or BeagleBone boards.
You can use the wget command if you want to download a file from a server to your local computer:
The command will attempt to download the resource you supplied and it will automatically detect the file extension. For example: If you point it to a website, it’ll download an HTML file. If you’re transferring a large file, you can simply log out and the tool will finish the task in the background.
How to Use Process Management
ps / top
The ps / top commands allow you to list the processes running on your computer:
Note that this will only generate a snapshot. You can, however, get a list that refreshes itself by using the top command:
The kill command can be used to end a running task. You may want to end a program that’s either not responding or that’s running in the background. Either way, you’ll have to find out the PID of the process (you can do that with ps or top):
The service command can be used to manage services on your computer. It’s quite versatile in a way that you can start, stop, and restart a service as well as print details and a list of all available services on your computer. To print a list, use the following command:
To print a detailed status of a particular service, use:
service <service_name> status
As you can see, the service is currently running. That means we can stop or restart it. To restart it, type:
sudo service bluetooth restart
To end the service, replace restart with stop.
How to Mounting and Unmount USB Drives
lsblk / fdisk
You can use the lsblk and fdisk commands to list block devices (lsblk) and to display and manipulate disk partitions (fdisk). Either one will work for this task.
As you can see, I ran the command before and after inserting a USB drive. Next, take note of the partition you’d like to mount. In my case, that’s sda2.
Use this command to mount the filesystem of an external drive in your local file system. This will allow you to work with the files on the storage device.
sudo mount <partition_to_mount> <local_mounting_point>
sudo mount /dev/sda2 /mnt
You can then access the files on the mounted partition by navigating to the /mnt folder.
After you’re done with the USB drive, you need to eject it. You can do that by using the unmount command together with the mounting point, you defined earlier.
sudo umount <local_mounting_point>
These commands are just a small percentage of what the Linux bash has to offer. However, I find these to be particularly useful in everyday life, especially when working on a remote computer via SSH or a headless system.
How to Use Concatenating Commands
The true power of the Linux shell doesn’t come from using simple commands and programs. You can still get a lot done like that, but to work even more efficiently and fast, you can concatenate commands instead of waiting for one to finish before manually typing in the next one. Furthermore, you can filter or store the results in a file which can be handy in many ways.
How to Filter Results
Some commands, like ps or ls, can give you a very long list of results. Usually, you’re only interested in a particular entry or a set of entries (for example all .py files in a folder). A command, that allows you to search for specific characters in a string, is called grep. It will simply print all lines where the string you’ve searched for appears.
Now, you’ll only need to feed the output of a program, for example, echo or ls, into the grep command as an input and it’ll filter everything irrelevant. As you can see, the pipe operator is used to accomplish that. Let’s run ls as an example:
I’m only interested in the files that end with .py. To apply a filter, I feed the output of ls into grep with a fitting regular expression:
How to Run a Program in the Background
Sometimes you’d like to execute a program that might take a while to complete but you don’t want to wait for it to finish. You can use an ampersand for that purpose.
That will run in the program in the background. When it starts, the PID of the new process gets printed to the console and everything, the program outputs to stdout, will also get printed to the console.
How to Write to and Read From Files
The results of all programs that output to the stdout stream, can be written to other programs or files. For example, if I wanted to store the list of python scripts in a file, I could do so by typing:
ls -l | grep ".*.py$" > python-scripts.txt
This will create (or overwrite) a file called python-scripts.txt that contains the results of the ls command filtered by grep. If you don’t want to overwrite the contents of an existing file, you can use >> to append new text to it instead.
If you want to feed the contents of a file into a program, you can use the < symbol.
These operators will write to the standard input stream and read from the standard output of a program respectively.
How to Run Multiple Commands at Once
Let’s suppose you want to run multiple commands that might take a while to finish. But you don’t want to sit at your computer and wait for them to finish just so that you can enter the next command. Luckily, you can enter a bunch of instructions at once and they’ll be executed one after another while you can enjoy a coffee break.
To accomplish that, write all commands in one line and separate them with semicolons:
But what if a command fails? Or you only want to execute a command once you can be sure that its predecessor was successful? Those cases are also covered.
A || B
This will run the command A and, if it fails, it will execute B. The following snippet does the exact opposite.
A && B
This means, that B will only get executed when A was successful. That way, you can string together quite complex commands that almost act like small applications themselves.
find py-files.txt && rm py-files.txt; touch py-files.txt && ls -a | grep ".*.py$" >> py-files.txt && echo "File created! The contents are:" && cat py-files.txt
Which will produce the following output: