The ls
command is used to list the contents of a directory. It can show files, directories, and their details such as permissions, ownership, size, and modification time.
ls [options] [directory]
options
: Various options to customize the output, e.g., -l
for long format, -a
to show hidden files.directory
: The directory whose contents you want to list. If not specified, it lists the contents of the current directory.# List the contents of the current directory in long format
ls -l
Output example:
total 8
drwxr-xr-x 2 user user 4096 Jun 1 10:00 dir1
-rw-r--r-- 1 user user 123 Jun 1 10:01 file1.txt
Use the -h
option with -l
to display file sizes in a human-readable format:
ls -lh
The cd
command is used to change the current working directory. It allows you to navigate through the file system.
cd [directory]
directory
: The directory you want to change to. It can be an absolute path or a relative path.# Change to the home directory
cd ~
# Change to a sub - directory named 'project'
cd project
Use cd -
to switch back to the previous directory.
The pwd
command prints the absolute path of the current working directory.
pwd
# Print the current working directory
pwd
Output example:
/home/user/project
Use it in scripts to ensure that the script is operating in the correct directory.
The mkdir
command is used to create new directories.
mkdir [options] [directory]
options
: Options like -p
to create parent directories if they don’t exist.directory
: The name of the directory to create.# Create a single directory named 'new_dir'
mkdir new_dir
# Create a nested directory structure
mkdir -p project/src
Use the -p
option when creating nested directories to avoid errors if parent directories don’t exist.
The rm
command is used to remove files and directories.
rm [options] [file/directory]
options
: Options like -r
to remove directories recursively, -f
to force removal without confirmation.file/directory
: The file or directory to remove.# Remove a file
rm file1.txt
# Remove a directory and its contents recursively
rm -r dir1
Be extremely careful when using the -r
and -f
options, as they can permanently delete important data.
The cp
command is used to copy files and directories from one location to another.
cp [options] [source] [destination]
options
: Options like -r
to copy directories recursively.source
: The file or directory to copy.destination
: The location where you want to copy the file or directory.# Copy a file
cp file1.txt file2.txt
# Copy a directory and its contents
cp -r dir1 dir2
Use the -i
option to prompt for confirmation before overwriting an existing file:
cp -i file1.txt file2.txt
The mv
command is used to move files and directories from one location to another. It can also be used to rename files and directories.
mv [options] [source] [destination]
options
: Options like -i
to prompt for confirmation before overwriting.source
: The file or directory to move or rename.destination
: If it’s a directory, the file or directory is moved there. If it’s a file name, the file is renamed.# Move a file to a different directory
mv file1.txt dir1
# Rename a file
mv file1.txt new_file.txt
Use the -i
option to avoid accidentally overwriting important files:
mv -i file1.txt new_file.txt
The grep
command is used to search for a specified pattern in a file or a stream of text. It prints the lines that contain the pattern.
grep [options] [pattern] [file]
options
: Options like -i
for case - insensitive search, -r
to search recursively in directories.pattern
: The pattern you want to search for. It can be a simple string or a regular expression.file
: The file or files in which you want to search.# Search for the word 'error' in a file named 'log.txt'
grep 'error' log.txt
# Search recursively for the word 'function' in all files in a directory
grep -r 'function' project/
Use regular expressions to perform more complex searches. For example, to search for lines starting with ‘INFO’:
grep '^INFO' log.txt
The chmod
command is used to change the file permissions of a file or directory. It allows you to control who can read, write, and execute the file.
chmod [options] [permissions] [file/directory]
options
: Options like -R
to change permissions recursively.permissions
: Can be specified in symbolic or numeric form.file/directory
: The file or directory whose permissions you want to change.# Give read, write, and execute permissions to the owner of a file
chmod u+rwx file1.txt
# Give read and execute permissions to the group and others for a directory recursively
chmod -R g+rx,o+rx dir1
Use numeric permissions for more precise control. For example, chmod 755
gives read, write, and execute permissions to the owner and read and execute permissions to the group and others.
The ssh
command is used to establish a secure connection to a remote server. It allows you to execute commands on the remote server securely.
ssh [options] [user@]hostname
options
: Options like -p
to specify a different port.user
: The username on the remote server.hostname
: The IP address or hostname of the remote server.# Connect to a remote server with the username 'user' and hostname 'example.com'
ssh [email protected]
Use SSH keys for authentication instead of passwords for better security. Generate SSH keys using ssh-keygen
and copy the public key to the remote server using ssh-copy-id
.
Mastering these top 10 Linux commands is essential for every developer. They form the foundation of working efficiently in a Linux environment, whether you’re developing software, managing servers, or debugging issues. By understanding their fundamental concepts, usage methods, common practices, and best practices, you can become more productive and confident in your Linux development workflow.
man [command]
in the terminal.This blog provides a comprehensive overview of the top 10 Linux commands that developers should know. However, the Linux command - line is vast, and there are many more commands and options to explore.