Last updated on July 12th, 2023.
Are you new to the world of Linux terminal commands and feeling a bit overwhelmed? Don't worry, our comprehensive command line guide is here to help you navigate the intricacies of the linux terminal with ease. Whether you're a beginner seeking to familiarize yourself with the Linux terminal or an experienced user looking to expand your knowledge, this guide will provide you with essential command line tips, detailed instructions, and practical examples. By the end of this guide, you'll be equipped with the necessary skills to confidently execute linux commands and unleash the full potential of the Linux command line.
Opening a terminal
Many linux based operating systems include a Terminal, in Windows it is referred to as the Command Prompt. This is an application with a very basic window in which you can type commands that the computer knows what to do with. To open the Terminal, some operating systems provide the key combo Ctrl-Alt-T. If that doesn't work, open the Menu on your desktop. It may be under Accessories, System or one of the other Menu categories.
The Terminal will have an appearance similar to this:
Basic Commands
ls - The list command
The list command will show you the list of files in a folder (directory).
The results of the ls command.
pwd - The pwd command
The pwd command will show you the current directory that your Terminal is in.
The results of the pwd command.
cd - The cd command
The cd command is used to change the current working directory in the command line interface. The term "cd" stands for "change directory." By using the cd command followed by the name of a directory, you can navigate to that directory and make it the current working directory.
Here are a few common ways to use the cd command:
Change to a specific directory:
This command will change the current directory to the one specified by the absolute path /home/user/ (where user is your username).
Change to the user's home directory:
Simply typing cd without any arguments will change the current directory to the user's home directory.
Move up one directory level:
This command will change the current directory to the parent directory (one level up) of the current directory.
Move to the previous directory::
This command will change the current directory to the previous directory you were in. It is useful for quickly switching between two directories.
cp - The cp command
The cp command is used to copy files and directories from one location to another. It stands for "copy."
Here, SOURCE refers to the file or directory you want to copy, and DESTINATION is the location where you want to copy it.
Copying a file to a different location:
cp source_file destination_directory/
Copying multiple files to a directory:
cp source_file1 source_file2 ... destination_directory/
Copying a directory and its contents to a new location:
cp -r source_directory/ destination_directory/
Preserving file attributes:
cp -a source_file destination_directory/
The -r option is used to copy directories recursively, and the -a option preserves the file attributes, such as timestamps and permissions, during the copying process.
It's important to note that if a file with the same name already exists in the destination location, the cp command will overwrite it by default. To avoid overwriting, you can use the -i option to prompt for confirmation before overwriting existing files.
mv - The mv command
The mv command is used to move or rename files and directories. It allows you to change the location of a file or directory within the file system or give it a new name.
Here are some common uses of the mv command:
Move a file or directory: You can use the mv command to move a file or directory from one location to another. For example:
mv file.txt /path/to/destination/
mv directory /path/to/destination/
Rename a file or directory: By specifying a new name as the destination, you can use the mv command to rename a file or directory. For example:
mv oldname.txt newname.txt
mv olddir newdir
Move multiple files or directories: You can move multiple files or directories at once by specifying multiple source files/directories and a destination directory. For example:
mv file1.txt file2.txt directory/
Move files into another directory while preserving the filename: If you want to move multiple files into a single directory while keeping their original filenames, you can use a wildcard (*) as the destination. For example:
mv *.txt directory/
It's important to note that the mv command is a powerful tool, and if used incorrectly, it can result in unintended data loss. Always double-check your commands and ensure you have appropriate permissions before moving or renaming files.
mkdir - The mkdir command
The mkdir command is used to create a new directory (also known as a folder) within the file system. The term "mkdir" stands for "make directory." When you run the mkdir command followed by the name of the directory you want to create, it will create that directory in the current location or at the specified path.
Here's the basic syntax of the mkdir command:
mkdir [options] directory_name
-p: This option allows you to create parent directories as well if they don't exist. For example, if you want to create a directory called "parent/child/grandchild" and the "parent" and "child" directories don't exist, you can use mkdir -p parent/child/grandchild to create all three directories at once.
--m: This option is used to set the permissions (mode) for the created directory. You can specify the permissions in octal notation. For example, mkdir -m 755 mydir will create a directory called "mydir" with permissions set to read, write, and execute for the owner, and read and execute for others.
---help: This option displays the help message for the mkdir command, showing all available options and their descriptions.
Here are some examples of how you can use the mkdir command:
Create a directory named "myfolder" in the current location:
Create a directory named "newdir" inside an existing directory:
mkdir existing_dir/newdir
Create multiple directories at once using the -p option:
mkdir -p parent/child/grandchild
Create a directory with specific permissions:
mkdir -m 755 mydir
rmdir - The rmdir command
The rmdir command is used to remove empty directories (folders) from the file system. The name "rmdir" stands for "remove directory."
When you run the rmdir command followed by the name of a directory, it will only delete the directory if it is empty, i.e., it does not contain any files or subdirectories. If the directory is not empty, rmdir will display an error message and refuse to remove it.
Here's the basic syntax of the rmdir command:
rmdir [options] directory_name
Some common options that can be used with rmdir are:
-p or --parents: Remove parent directories as well if they become empty after removing the specified directory.
-v or --verbose: Display a message for each directory that is successfully removed.
--ignore-fail-on-non-empty: Remove the directory even if it is not empty (not recommended unless you are sure about what you're doing).
It's important to exercise caution when using the rmdir command because it permanently deletes directories. Always double-check the directory you intend to remove and make sure it doesn't contain any important files or subdirectories.
chmod - The chmod command
The chmod command is used to change the permissions of files and directories. The name "chmod" stands for "change mode." It allows you to modify the read, write, and execute permissions for the owner, group, and others (everyone else) on a file or directory.
Here's the basic syntax of the chmod command:
chmod options permissions file(s)
Here, options are optional flags that modify the behavior of the command, permissions specify the new permissions you want to set, and file(s) represent the file or files you want to change the permissions for.
The permissions parameter is typically expressed using a three-digit octal number or symbolic notation. Octal notation uses numbers from 0 to 7, where each digit corresponds to the permission type: read (4), write (2), and execute (1).
The digits are added together to represent the desired permission combination. Symbolic notation, on the other hand, uses letters and symbols to represent permissions.
For example, to give read, write, and execute permissions to the owner, read and execute permissions to the group, and read-only permissions to others on a file named "example.txt" you could use the following command:
chmod 754 example.txt
Here's a breakdown of the permissions:
7: Read (4) + Write (2) + Execute (1) for the owner
5: Read (4) + Execute (1) for the group
4: Read-only for others
The chmod command provides various options and symbolic notations to fine-tune permissions, such as u for the owner, g for the group, o for others, + to add permissions, - to remove permissions, and = to set permissions explicitly.
chown - The chown command
The chown command is used to change the ownership of files and directories. The word "chown" stands for "change owner." It allows you to modify the user and group ownership of a file or directory to a specific user and group.
The general syntax of the chown command is as follows:
chown [OPTIONS] [OWNER][:GROUP] FILE
Here's a breakdown of the components:
[OPTIONS]: You can use various options with the chown command to modify its behavior. Some commonly used options include -R (recursive) to change ownership recursively for directories and their contents, and -v (verbose) to display detailed output.
[OWNER]: Specifies the new owner of the file or directory. You can provide the username or the numeric user ID (UID).
[:GROUP]: Optional specification of the new group for the file or directory. If not provided, the group ownership remains unchanged. Similar to the owner, you can use either the group name or the numeric group ID (GID).
FILE: Specifies the file or directory you want to change ownership for.
Here are a few examples of using the chown command:
Change the owner of a file:
chown user1 file.txt
Change the owner and group of a directory recursively:
chown -R user2:group2 directory/
Note that the chown command typically requires root privileges (sudo) to change ownership of files or directories that you don't own.
touch - The touch command
The "touch" command is used to create a new file or update the timestamp of an existing file. The primary purpose of the command is to update the access and modification times of a file. If the file specified doesn't exist, the touch command creates an empty file with the given name.
Here are some common use cases of the touch command:
Create a new file: If you provide a filename that doesn't exist, the touch command will create a new empty file with that name. For example:
touch myfile.txt
Update access and modification times: If you want to update the access and modification times of an existing file without modifying its content, you can use the touch command. For example:
touch myfile.txt
Create multiple files: The touch command allows you to create multiple files at once. You can provide multiple filenames as arguments, separated by spaces. For example:
touch file1.txt file2.txt file3.txt
Set a specific timestamp: By default, the touch command updates the access and modification times to the current time. However, you can also specify a specific timestamp using the "-t" option followed by the desired time in the format "YYYYMMDDHHMM.SS". For example:
touch -t 202105301200.00 myfile.txt
Update timestamp of symbolic links: By default, the touch command updates the timestamp of the file itself. However, if you want to update the timestamp of a symbolic link, you can use the "-h" option. For example:
touch -h myfile.txt
rmdir - The rmdir command
The rmdir command is used to remove empty directories (folders) from the file system. The name "rmdir" stands for "remove directory."
When you run the rmdir command followed by the name of a directory, it will only delete the directory if it is empty, i.e., it does not contain any files or subdirectories. If the directory is not empty, rmdir will display an error message and refuse to remove it.
Here's the basic syntax of the rmdir command:
rmdir [options] directory_name
Some common options that can be used with rmdir are:
-p or --parents: Remove parent directories as well if they become empty after removing the specified directory.
-v or --verbose: Display a message for each directory that is successfully removed.
--ignore-fail-on-non-empty: Remove the directory even if it is not empty (not recommended unless you are sure about what you're doing).
It's important to exercise caution when using the rmdir command because it permanently deletes directories. Always double-check the directory you intend to remove and make sure it doesn't contain any important files or subdirectories.