The command line can be an intimidating and scary black screen when first introduced to beginners. Where are the fancy buttons? The colors? How can I move files? Where is my mouse!?
These 5 Linux Commands will give you a better handle of the command line and will make you look like a pro.
1. "cp
"
The cp
command stands for "copy" and the syntax of the cp
command works like this:
$ cp [OPTIONS] SOURCE DESTINATION
Where the SOURCE
is copied over to DESTINATION
, you can also have multiple SOURCE
files, so that you can copy over multiple files to DESTINATION
. A useful flag that you may end up using a lot is the -R
, -r
, or --recursive
command, which allows you to cp entire directories recursively. For example:
2. "mv
"
The mv
command otherwise known as "move" will move files wherever you want them to go, similar to the cp
command, but instead of leaving a copy the entire file or directory will get moved to the DESTINATION
. Also, in the man
docs mv
it shows, mv - move (rename) files
. So, not only can you move files, but by using the mv
command like this:
$ mv [FILE] [RENAMED]
A useful flag to understand what is going on while using the mv
command is the -v
command, which stands for verbose. When you use the mv
command, use a -v
before your SOURCE
input to show a more detailed view of what the mv
command is doing. For example:
3. "|
"
The |
command, aka the "pipe" command is used to combine two or more commands where the first command will be an input for the second command. (And so on so forth) Seeing the "pipe" in action makes it easier to understand.
In this block of code, the contents of cat are shown, then you would use the |
command to use the contents of file.txt
and run the command sort
on them, which alphabetically sorts the contents of file.txt
and displays those results on the console.
4. >
The right-angle bracket is a way to transform and push data in your terminal from one place to another. Just like the example from the pipe command you can store the data of the sorted file.txt
to another file called sorted_file.txt
, so:
First, you sorted checked the file contents of file.txt
and then you used the pipe
command along with the sort
command. From above, you saw that this displayed the contents of file.txt
but alphabetically sorted, now with the >
command, you take those contents of the alphabetically sorted file and put that data into a new file called sorted_file.txt
. Neat right?
The
>
and|
may not be super useful by themselves, but when used with other commands they are very powerful tools that you can use to your advantage.
5. grep
From the man
docs grep
prints lines that match a pattern that you specify. The usage is as follows:
$ grep [OPTION...] PATTERNS [FILE...]
A simple usage for the grep
command could be to find all lines that have an "a" in a file. For example:
You can extend the grep
command to look for certain files, combining the grep
command with the |
command:
Using these commands in unison with each other will give you a better grasp at the Linux terminal, and make you look like a pro to your friends and co-workers.
BONUS: tree
The tree
command is not a preinstalled command available in Linux, although, it is a nifty tool that can help you find files in your system. To install tree
run
$ sudo apt install tree
For Ubuntu systems, although other systems may use different package managers. tree
lists contents of a directory in a tree-like format. For basic usage you could run
$ tree folder
For example:
Then, using your new founded knowledge of the "pipe" command and "grep" you can filter and find specific files that you want, for example, searching for all .cpp
files.
$ tree labs | grep .cpp
Hope you found this useful!
Top comments (0)