In this section we are going to learn some basic navigation commands and how to manipulate files using the command line. These commands will be very useful when you are working with modern technology stacks like JavaScript’s npm or PHP’s Composer. If you don’t know what JavaScript or PHP is, don’t worry about it for now.
If you are using Ubuntu, open your file explorer and navigate to home from the left sidebar.
Now open the Terminal and type this command.
$ ls
That’s right, only two letters ls
and hit Enter.
Practice makes perfect, and by this example I hope you already understand that ls
command, which is short for “list”, will make a list of files and directories in your current directory and print it to the screen. You will notice that most commands are shortened because they are typed over and over. The blue colored text indicates a directory, while the regular white text are for regular files. Here is the list of text colors and their meaning:
- Blue: Directory
- Green: Executable or recognized data file
- Sky Blue: Symbolic link file
- Yellow with black background: Device
- Pink: Graphic image file
- Red: Archive file
- Red with black background: Broken link
- White: Regular files (e.g. txt or doc files)
Create Files
In order to create a file from the terminal, you can type the following command.
$ touch text.txt
$ touch document.doc
$ touch spreadsheet.xsl
As you can see by these examples, you are bound to type the same command over and over to make files or list the content of your directory because you forgot. To make things simpler, the terminal can duplicate your previous commands for execution by pressing the “up-arrow” [↑] key in your keyboard. Now try to create another file named “slide.ppt” by pressing up key in your keyboard, and you’ll see what I mean. It comes in really handy while working with command line.
Searching A File
This touch
second command will create an empty file in the current directory that you can edit later. The listing command ls
also support for searching the current directory with wildcard character *
(you can read as “star”). For now, let’s try searching for a .xsl file. Type the following command and press Enter.
$ ls -l *.xsl
You will see ls
printed the spreadsheet.xsl into the terminal along with some file details. Now you might be wondering, what’s that -l
after the ls
command? It’s called optional argument that you can include into Unix commands. The -l
means “do list with a long listing format” in human language. To check any parameters supported by ls
, you can use --help
optional argument to display help information.
$ ls --help
Most Unix commands have their own help arguments that will display information relevant to the command you typed, with the exception of echo
, which will only print back the --help
characters. You can also try help for touch
command. Now don’t be overwhelmed with the amount of information printed by help arguments. You don’t need to know all of the available arguments for a command, and the purpose of this guide is to tell you the most relevant arguments you will be using daily when working with computers.
the ls
also has -a
argument (short for “all”) which is commonly used to display hidden files.
Many operating systems and application programs routinely hide files and directories in order to reduce the chances of users accidentally damaging or deleting critical system and configuration files. Hiding these objects can also be useful for reducing visual clutter in directories, and thereby making it easier for users to locate desired files and subdirectories. As a developer who works on making software, you will encounter some hidden files and directories in your daily routine. Let’s learn how to make one.
$ touch .hidden_file
$ mkdir .hidden_file
You have just learned one more command with the example! The mkdir
command is short for “make directory”, and you have just made a hidden directory with it. Hidden objects in Unix-like system are easily distinguishable from regular objects, because all hidden files or directories are prefixed by a dot .
now try to run ls -la
command and see what you got. Your results will of course vary from mine.
Next, let’s make the hidden files visible. You can type this command.
$ mv .hidden_file hidden_file
Moving, Copying and Deleting Files
The mv
command is short for “move”, and it’s actually used to move files around like cut and paste, but for this example we just use it to rename the file. If you look at your explorer window, the “hidden_file” is now visible. Let’s do another run.
$ mv document.doc Documents/mydoc.doc
We have just moved the document.doc file into Documents folder and rename it into mydoc.doc, which is equivalent to cut and paste and then rename file in graphical interface.
Now what about copying files?
Of course we can also do it easily with cp
command, short for “copy.”
$ cp text.txt Documents/text_backup.txt
Now we have a backup of text.txt inside the Documents folder. You can copy into the same directory by omitting the Documents/ part.
Now that we have covered, create, copy, and move file.. Are you interested in how to delete them? It’s very simple indeed!
$ rm text.txt
There you go. The text.txt file is gone from the home directory. Can you guess what rm
is short for? I won’t tell you because it’s pretty obvious. Hahaha.
Extra Commands For Playing With File
Now another question you might come up with is we can create files, but how to edit or view their content? Editing content of a file is actually not a terminal’s specialty, because we’re going to need a text editor for that. There are some quick ways to view and insert content to a file, but that is the extend of command line’s ability to interact with files. Let’s see some example of inserting content into a file by the following command.
$ echo "The quick brown fox jumps over the lazy dog." > text.txt
The >
sign means “redirect operator” in the command line. So in this case, we are redirecting the sentence to be printed in a file named text.txt.
But didn’t we just delete that file? Great question! The redirect command search for a file named text.txt, and when not found, create one! To view the content of text.txt file, you can use the following command.
$ cat text.txt
The quick brown fox jumps over the lazy dog.
Nope, cat
is not an animal in the command line world. It’s a short for “concatenate”. Originally the purpose of cat
is to combine the content of multiple files into one and print it to the screen. But cat
also works fine with just a single file as its input.
If you wish to try cat
, here are some commands you can try:
$ echo "The dog gets angry at the brown fox." > text_2.txt
$ echo "A chase between dog and fox begins." > text_3.txt
$ cat text.txt text_2.txt text_3.txt
The quick brown fox jumps over the lazy dog.
The dog gets angry at the brown fox.
A chase between dog and fox begins.
One thing to note is that cat
did not join the content permanently, so if you cat
the text.txt again, the content is still just one sentence.
The command line also support putting another line into a file by using two right angle brackets >>
, also know as “append operator”, which means the sentence is appended to the end of line.
$ echo "The dog gets angry at the brown fox." >> text.txt
$ echo "A chase between dog and fox begins." >> text.txt
$ cat text.txt
The quick brown fox jumps over the lazy dog.
The dog gets angry at the brown fox.
A chase between dog and fox begins.
These extra commands are rarely used in a real job, so you’ll use it very rarely, if not at all.