Basic commands
This page covers basic commands that you can use in the terminal. These commands are essential for navigating the file system, managing files, and executing programs.
Definitions
- A directory is a folder that contains files and other directories. The root directory is the top-level directory in the file system. Directories can contain files and other directories, creating a hierarchical structure.
- A file is a collection of data stored on a computer. Files can contain text, images, programs, or other types of data.
- A path is a string of characters that specifies the location of a file or directory in the file system. There are two types of paths: absolute paths and relative paths.
- An absolute path specifies the full location of a file or directory from the root directory. For example,
/home/user/Documents/file.txtis an absolute path. - A relative path specifies the location of a file or directory relative to the current working directory. For example,
Documents/file.txtis a relative path that refers to a file namedfile.txtin theDocumentsdirectory in the current working directory.
- An absolute path specifies the full location of a file or directory from the root directory. For example,
pwd - Print working directory
The pwd command is used to print the full path of the current working directory. The working directory is the folder you are currently in. It shows you where you are in the file system.
When you open a terminal, you start in your home directory. You can use the pwd command to see the full path to your home directory.
pwdExample output:
Linux:
/home/dhruvamacOS:
/Users/dhruvals - List files
The ls command lists the files and directories in the current directory. By default, it displays the names of files and directories in a single column.
lsTo display detailed information about files and directories, you can use the -l option:
ls -lcd - Change directory
The cd command is used to change the current directory (folder). You can specify the directory you want to change to as an argument.
cd <directory_name>Example:
cd Documentsmkdir - Make directory
The mkdir command is used to create a new directory. You can specify the name of the directory you want to create as an argument.
mkdir <directory_name>cp and mv - Copy and move files
The cp command is used to copy files from one location to another. You need to specify the source file and the destination directory as arguments.
cp <source_path> <destination_directory>Example:
cp file.txt Documents/file.txtThe mv command is used to move files from one location to another. It works similarly to the cp command but removes the original file after moving it.
mv <source_path> <destination_path>Example:
mv file.txt Documents/file.txtrm - Remove files
The rm command is used to remove files. You need to specify the file you want to delete as an argument.
rm <file_path>sudo - Superuser do Caution
We trust you have received the usual lecture from the local System Administrator. It usually boils down to these three things: #1) Respect the privacy of others.
#2) Think before you type.
#3) With great power comes great responsibility.
The sudo command is used to run a command with superuser (administrative) privileges. It allows you to perform tasks that require elevated permissions. Use it with caution.
sudo <command>