This will print out the current working directory that you’re in. It will usually be something like /home/user
.
This is the command used for navigating through the directories. You can either input the absolute path or relative to your current directory.
Let’s say our current directory is /home/user
, and we have a folder named “a” in there.
cd a
or cd /home/user/a
will take us to /home/user/a
cd ..
will move one directory upcd -
will move to the previous directoryUse this to print out the contents in your current directory1. So if we have the following files in our home directory: a.txt, b.txt, c.txt
ls
will print out a.txt b.txt c.txt
.
This command has a couple of command options. Here are a few important ones.
ls -a
: list all files including hidden onesls -l'
: list all files in long format (includes information like date created and permissions)2ls -R
: recursively lists all files in directory and subdirectoryls -t
: orders the files and directories by datels -r
: returns the order in reverseYou can also combine the commands, such as ls -la
, which will list all files including the hidden ones in long format.
This is used to create and delete folders. Just append the folder name at the end, like mkdir foldername
.
When creating a directory that includes other parent directories that don’t exist yet, such as /usr/a/b/c (assuming /a and /b directories don’t exist), you can use the -p
option to create the parent directories.
mkdir -p /usr/a/b/c
will create the /c directory, along with the /a and /b parent directories.
Note: rmdir
only lets you remove empty directories. To remove all of the subdirectories that it contains, use rm -r foldername
.
This is mostly used for moving files around, but it can also be used for renaming files.
To move a file, simply write the file name, followed by the destination path. mv filename.txt /home/user/a
.3
To rename a file, write the old file name, followed by the new file name. mv oldfilename.txt newfilename.txt
Use this command to copy files to a target destination.
cp filename.txt /home/user/a
Use this to remove files and directories. As mentioned before, to delete a directory and all of contents or subdirectories that it contains, use rm -r
.
This command is used to create a new file. This can be anything from a simple txt file to an html file.
touch filename.txt
To create multiple files, you can use the following command:
touch filename{0..100}.txt
This will create the files ‘filename0.txt’, … , ‘filename100.txt’.
This is used to move some data into some file, usually some text.
echo 'text' > filename.txt
: Use a single greater-than sign to overwrite the file. So this will replace the old data with the new data.echo 'text' >> filename.txt
: Use two greater-than signs to add data to the file. New data will be added to a new line.If no filename is stated, e.g. echo 'hello'
, it will simply print out to the terminal.
Note: If you use a filename that doesn’t exist, a new file with the content inside will be created.
These are text editors in linux. But since nano is newer and easier to use, let’s focus on nano.
Typing nano filename.txt
will take you to editor mode. Make the necessary changes, and then save by pressing Ctrl+O, exit by pressing Ctrl+X.
You can see the other options you can use on the bottom of the terminal window.
If you want to know what kind of content is inside a file, use cat
to print out the content. Simply type cat filename.txt
.
You can create a new file by using cat > newfilename.txt
. You can then type the content you want into the file, then exit by pressing Ctrl+Z.
Use this command to find certain words or phrases within a text.
For instance, if we want to find if the word “hello” exists somewhere in the file, we can use grep hello filename.txt
.
This will then display all the lines that contain the word.
For phrases, place them inside quotes, like grep 'target phrase' filename.txt
.
sudo
stands for “SuperUser Do”. Use this to run commands with administrative or root privileges.
This is used for changing the read, write, and execute permissions of files and directories. I’ll provide more info about this in a separate post.
Use this command to compress and extract tar files.
This is the basic syntax.
tar <operation><options> <archive_name> <files_or_location>
Here are some of the operations and options you can use.
Command | Type | Description |
---|---|---|
-c | operation | Creates new archive |
-x | operation | Extracts archive |
-t | operation | Lists archive’s contents |
-v | option | Sets to verbose mode |
-f |
option | Specifies the archive name |
For example:
# compress contents from directory_name into tarfilename.tar
tar -cvf tarfilename.tar directory_name
# extract contents from tarfilename.tar
tar -xvf tarfilename.tar