Good day! Do you remind the Command Line? In the last entry I wrote about the first steps with the command line. In this second episode, I will go in deep with commands like ‘ls’ and analyze other new commands like ‘cp’, ‘mv’ and the Wildcards.
Before I go into detail, let’s take a little reminder about the Episode I:
• pwd
outputs where we are: the name of the working directory.
• ls
list the files and directories in the working directory.
• cd
switches the folder where you are to the directory you specify.
• mkdir
creates a new folder in the working directory.
• touch
creates a new file inside the current working directory.
• rm
removes files.
• rm -r
removes directories.
More about Command LS
I’ll start explaining about some options that can modify the behavior of the command ‘ls’.
◦ ls -a
: will list all contents of the working directory, including hidden files and folders.
◦ ls -l
: lists all the contents vertically.
◦ ls -t
: will order files and directories by the time they were last modified.
◦ ls -m
: will display names in a single line, with commas separating files and folders.
You can use multiple options, like ls -alt
.
There are plenty of options, but this four are the most used for me.
The Command MV
The next command, ‘mv’ , is used to move and rename files and directories. You can do both actions in the same command.
Example 1: Move a file into a folder
$ ls
anakin.js light darkness
$ mv anakin.js light
// This will move anakin.js into light/anakin.js
Example 2: Change name and move file into another folder in the same action
$ mv light/anakin.js ../darkness/darth-vader.js
// This will change the file’s name from anakin.js to darth-vader.js and change its folder
The Command CP
The ‘cp’ command is similar to ‘mv’, you can copy files or directories and change its name in the same command.
Example 1: Copy a folder into another folder
$ ls
darkness light force
$ cp force light/
// This command will copy force into ‘light’ folder
The Wildcards
Now, I’ll introduce Wildcards. The Wildcards can help selecting groups of files and directories.
A wildcard is a simple character that can be used as a substitute for another character. The term wildcard was originally used in card games by the player who wanted a card to have another value.
Star Wildcard (‘*’): Some examples using the ‘*’ wildcard with ‘ls’ and ‘cp’.
Example 1: Star Wildcard ‘*’ after character:
$ ls
man mean men
$ ls me*
mean men
Example 2: Star Wildcard ‘*’ before character:
$ ls
man mean men
$ ls *en
men
Example 3: Star Wildcard ‘*’ using ‘cp’:
$ ls
padawan.js sith.js jedi.js
$ cp p*.js school/
// This command will copy all files that begin with ‘p’ and end with ‘.js’ into the folder ‘school’. In this case, ‘padawan.js’ is the only file that will be copied
Question Mark Wildcard (‘?’): Some examples using the ‘?’ wildcard with ‘ls’.
Example 1: Question Mark Wildcard knowing characters:
$ ls
padawan.js sith.js jedi.js   light darkness
$ ls ???????
sith.js jedi.js
// Seven ‘?’ will return all files with 7 characters, inclusive of any extensions.
Example 2: Question Mark Wildcard with Star Wildcard:
$ ls
padawan.js sith.js jedi.js   light darkness
$ ls *.??
padawan.js sith.js jedi.js
// This command would return a list of all files in the current directory that have a two-character filename extension
Square Brackets Wildcard (‘[]’): Some examples using the ‘[]’ wildcard with ‘ls’.
Example 1: Square Brackets Wildcard for extensions:
$ ls *.[abc]*
// This Wildcard would list all files that had an extension that begins with ‘a’, ‘b’ or ‘c’
As you’ve seen, Wildcards are similar to Regular Expressions. There a lot of ways to use them, so practice without fear.
One thought on “The Command Line: Episode II”