Linux is a powerful and diverse operating system, so naturally, the command line offers a wealth of power and functionality. If you’re starting on your journey toward Linux mastery, here are a few of the most commonly used commands that will carry you a long way.
File Operations
ls
: Lists all files and directories in the present working directory. You can use flags like-l
for long listing,-a
to show hidden files, and-h
to show file size in human-readable format.cd
: Changes the current directory. You can usecd ..
to go one directory up,cd /
to go to the root directory.pwd
: Displays the present working directory. It shows the full path starting from the root directory.touch
: Creates a new file. For example,touch newfile.txt
will create a new file namednewfile.txt
.cp
: Copies files from the source directory to the destination directory. For example,cp source.txt destination.txt
will copysource.txt
todestination.txt
.mv
: Moves files between directories, or renames a file. For example,mv oldname.txt newname.txt
will renameoldname.txt
tonewname.txt
.rm
: Deletes a directory or a file. Be careful with this command, as the deleted files cannot be recovered. Use-r
flag to remove directories.find
: Searches for files and directories. For example,find /home -name myfile.txt
will search formyfile.txt
in the/home
directory.
Process Management
ps
: Displays your currently active processes. Useaux
flags to view all running processes on the system.top
: Displays all running processes and their usage of system resources like CPU and memory.kill
: Kills a process. You need to know the PID (Process ID) of the process to kill it.bg
: Sends a process to the background. This is useful if you have a process that you want to keep running while you do other things.fg
: Brings a process to the foreground. If you have a process running in the background, you can bring it to the foreground with this command.
File Permissions
chmod
: Changes the permissions of a file or directory. Permissions are defined as a three-digit code, where each digit is an integer between 0-7.chown
: Changes the owner and group of a file or directory. For example,chown user:group file.txt
will change the owner offile.txt
touser
and the group togroup
.chgrp
: Changes the group of a file or directory. For example,chgrp group file.txt
will change the group offile.txt
togroup
.
Networking
ping
: Sends an ICMP echo request to establish a connection to server/IP. This is useful for troubleshooting network connectivity issues.netstat
: Displays network statistics. This is useful for checking your network connections, and for troubleshooting.ssh
: Secure Shell to get remote control of any Linux machine. For example,ssh user@hostname
will start a SSH session withhostname
asuser
.scp
: Securely copies files between hosts on a network. For example,scp source.txt user@hostname:/path
will copysource.txt
to/path
onhostname
asuser
.
Package Management
apt-get
: APT package handling utility (Debian). You can useapt-get install package
to install a package,apt-get remove package
to remove a package.yum
: Package manager (Red Hat). Similar toapt-get
, you can useyum install package
to install a package,yum remove package
to remove a package.dnf
: Next-generation package manager (Fedora). It’s the successor toyum
and uses the same syntax.
Disk Usage
df
: Shows disk usage. It displays the amount of disk space used and available on your filesystem.du
: Estimates file and directory space usage. For example,du -sh /path
will show the total disk usage of/path
in human-readable format.fdisk
: Disk partition manipulator. It’s a powerful tool for creating, deleting, resizing, and managing disk partitions.dd
: Converts and copies a file. It’s a low-level command that allows you to make exact copies of a file, convert data formats, and more.
Text Manipulation
cat
: Concatenates and displays file content. For example,cat file.txt
will display the content offile.txt
.more
: Views the contents of a file one screen at a time. This is useful for viewing large files.less
: Opposite of the ‘more’ command. It allows backward navigation in the file as well as forward navigation.head
: Displays the beginning of a file. For example,head -n 5 file.txt
will display the first 5 lines offile.txt
.tail
: Displays the end of a file. For example,tail -n 5 file.txt
will display the last 5 lines offile.txt
.sort
: Sorts the contents of a text file. For example,sort file.txt
will sort the lines infile.txt
.cut
: Removes sections from lines of files. For example,cut -d':' -f1 /etc/passwd
will display the first field of each line in/etc/passwd
.wc
: Counts lines, words, and characters in specified files. For example,wc -l file.txt
will count the number of lines infile.txt
.
System Information
uname
: Shows system information. For example,uname -a
will display all system information, including machine name, kernel name, version, and more.uptime
: Shows how long the system has been running. It displays the current time, how long the system has been running, how many users are currently logged on, and the system load averages for the past 1, 5, and 15 minutes.hostname
: Displays the system’s host-name. This is the name that the system is known by to other systems on the network.useradd
: Creates a new user. For example,useradd john
will create a new user namedjohn
.userdel
: Deletes a user account. For example,userdel john
will delete the userjohn
and his home directory.su
: Switches the user ID to another user. For example,su john
will switch the current session’s user tojohn
.sudo
: Executes commands as another user. For example,sudo apt-get update
will run theapt-get update
command with root privileges.passwd
: Changes the user password. For example,passwd john
will change the password for the userjohn
.
Compression / Decompression
tar
: Stores and extracts files from a tape or disk archive. For example,tar -cvf archive.tar file1 file2
will create an archive namedarchive.tar
fromfile1
andfile2
.gzip
: Compresses or decompresses named files. For example,gzip file.txt
will compressfile.txt
tofile.txt.gz
.gunzip
: Decompresses files. For example,gunzip file.txt.gz
will decompressfile.txt.gz
tofile.txt
.zip
: Package and compress files. For example,zip archive.zip file1 file2
will create a compressed archive namedarchive.zip
fromfile1
andfile2
.unzip
: Extract files from a ZIP archive. For example,unzip archive.zip
will extract all files fromarchive.zip
.
Searching
grep
: Searches the input files for a pattern. For example,grep 'pattern' file.txt
will search forpattern
infile.txt
.locate
: Finds files by name. For example,locate file.txt
will find all instances offile.txt
in the system.
These top 50 Linux commands provide a solid foundation for efficiently navigating and managing your Linux system. Experiment with them in your terminal to gain confidence and proficiency, gradually unlocking the full potential of the command line.