Linux is a powerful and versatile operating system, and the command line interface (CLI) is an essential tool for interacting with it. Here, we’ll go over 30 useful Linux commands that can serve as a handy cheat sheet for both new and experienced users.
ls– Lists the files and directories in the current directory. Example:lswill list all the files and directories in the current directory.cd– Changes the current directory. Example:cd Documentswill change the current directory to the Documents directory.mkdir– Creates a new directory. Example:mkdir new_directorywill create a new directory called “new_directory”.rmdir– Removes an empty directory. Example:rmdir old_directorywill remove the empty directory called “old_directory”.touch– Creates a new empty file. Example:touch new_file.txtwill create a new empty file called “new_file.txt”.cp– Copies a file or directory. Example:cp file.txt backup/will copy “file.txt” to the “backup” directory.mv– Moves or renames a file or directory. Example:mv file.txt Documents/will move “file.txt” to the “Documents” directory.rm– Removes a file. Example:rm old_file.txtwill remove the file called “old_file.txt”.cat– Displays the contents of a file. Example:cat file.txtwill display the contents of “file.txt” on the screen.less– Displays the contents of a file in a paginated format. Example:less file.txtwill display the contents of “file.txt” one page at a time.grep– Searches for a specific pattern in a file or multiple files. Example:grep "example" file.txtwill search for the string “example” in “file.txt”.find– Searches for files and directories in a directory hierarchy. Example:find / -name "example"will search for files and directories named “example” starting at the root directory.wc– Displays the number of lines, words, and characters in a file. Example:wc file.txtwill display the number of lines, words, and characters in “file.txt”.sort– Sorts the contents of a file. Example:sort file.txtwill sort the contents of “file.txt” alphabetically.uniq– Removes duplicate lines from a sorted file. Example:sort file.txt | uniqwill sort and remove duplicates from “file.txt”.cut– Removes sections from each line of a file. Example:cut -f 1 file.txtwill remove the second field from each line of “file.txt”.paste– Merges lines of files. Example:paste file1.txt file2.txtwill merge the contents of “file1.txt” and “file2.txt” into a single file.awk– Text processing tool for printing columns and rows from a file. Example:awk '{print $1}' file.txtwill print the first column of “file.txt”.tar– Archiving tool for creating, extracting, and manipulating tar files. Example:tar -cvf archive.tar file1 file2will create an archive called “archive.tar” with “file1” and “file2”.gzip– Compression tool for creating and extracting gzip files. Example:gzip file.txtwill create a compressed file called “file.txt.gz”.gunzip– Decompression tool for extracting gzip files. Example:gunzip file.txt.gzwill extract “file.txt.gz” and create a file called “file.txt”.chmod– Changes the permissions of a file or directory. Example:chmod 755 file.txtwill give read, write, and execute permissions to the owner, and read and execute permissions to everyone else for “file.txt”.chown– Changes the owner and group of a file or directory. Example:chown user:group file.txtwill change the owner and group of “file.txt” to “user” and “group” respectively.df– Displays the amount of free space on the file system.- Example:
df -hwill show the available disk space in a human-readable format du– Displays the disk usage of a directory or file. Example:du -sh /homewill show the disk usage of /home directory in human-readable formatfree– Displays the amount of free and used memory in the system. Example:free -hwill show the memory usage in a human-readable format-
sed– Stream editor for filtering and transforming text. Example:sed 's/old_word/new_word/g' file.txtwill replace all occurrences of “old_word” with “new_word” in “file.txt”. top– Displays real-time information about the system’s processes. Example:topwill show the list of running processes and their resource usage in real-timeps– Displays information about the currently running processes. Example:ps auxwill show a list of all running processes and their detailskill– Sends a signal to a process to terminate it. Example:kill -9 1234will force-terminate the process with ID 1234
These are just a few examples of the many powerful commands that Linux has to offer. By mastering these commands and understanding how to use them effectively, you can greatly increase your productivity and efficiency when working with the Linux operating system.