On my quest to stop being intimidated by the command line, the more I learn about it, the more fascinated I become by it.
In my last post ,which you can read here, I talked about the absolute basics anyone needs to get started using the terminal. But there is so much more you can do and in this post I'm going to dive deeper into not only viewing your filesystem in more detail but actually making changes to it.
You got options
We use ls
to list the contents of the current directory, however , we can use ls
with options which are usually preceded by the -
character .Options modify and change the default behaviours of the commands they are paired with. Let me explain:
-
ls -a
lists all contents of a directory, including hidden files and directories. Files starting with a dot.
are hidden and are not displayed with usingls
alone. -
ls -l
lists all contents of a directory in long format and displayed as a table. Each column represents access rights, hard links( number of child directories and files), the username ,the size of the file in bytes, the date and time the files were last modifies and the file's name. -
ls -t
orders files and directories by the date and time they were last modified.
Each option can be used separately or multiple options can be used together like -alt
. In this case -alt
lists all files and directories, including hidden ones in long format, ordered by the date and time they were last modified.
We can also use the command line to copy, move and remove files and directories
Copy
-
cp
copies files and directories. To copy a file into a directory we usecp
with the source file as the first argument and the destination directory as the second argument. If we have the filecodingbooks.txt
that's inside theBooks
directory and we want to copy it's contents into theCoding
directory, then we would do :
cp Books/codingbooks.txt Coding
To copy multiple files into a directory use cp
with a list of the files sources as the first arguments and the destination directory as the last argument:
cp Books/codingbooks.txt Books/ruby.txt Coding
Move
-
mv
works in the exact same way ascp
does. We use the file we want to move as the first argument and the destination directory as the second argument. In this case however, we can usemv
to also rename files . Say we want to renamenotes.txt
toNotes.txt
because we changed our mind and want for files to start with capital letters:
mv notes.txt Notes.txt
Remove
-
rm
removes files. In order to remove directories we userm -r
. The-r
is an option that stands forrecursive
.It modifies the command, similar to what we talked about earlier on, and deletes the directory and child directories. For example,to delete the Pictures directory:
rm -r Pictures
Be careful when using these commands because once you delete the files or directories, they are permanently deleted! You will not be able to retrieve them from the Bin in the GUI.
Wildcards
This one is the most powerful one in my humble opinion and the one that stood out to me the most. Using *
we can select multiple groups of files and directories. Instead of spending lot's of time dragging and dropping files in the GUI or typing in the terminal, if we know we want to select many files of a certain type or that are located in a certain directory, we can use this instead. Below I have a few examples of it's use :
- To list all files in the Documents directory, in long format, that end in
.pdf
ls -l Documents/*.pdf
- To copy all files that begin with
m
and end with.txt
in the current directory, to the Documents directory
cp m*.txt Documents
- To move all files in the working directory to the Pictures directory
mv * Pictures
- To delete all files in the Downloads directory
rm Downloads/*
Those are some of the ways we can manipulate and alter the file system using the terminal.
If you have made it this far, you deserve a cat gif