Useful Linux commands
About Commands
man
To get more information about a command type
which
Check where the command that will be used resides
This gives the path of the first occurence of command in the directories listed in the envrionment variable PATH. Note that a few commands are picked up directly by the shell, for example, the time command, and the program in the search path will not be executed. If a command behaves differently than expected from the man page, a solution may be to give the full path of the command found by which.
Manage Shell Variables and Parameters
echo
Give the value of a parameter
set
List the values of all defined environment variables alphabetically
export
Export a parameter to all future subshells
The value can be set and exported at the same time, for example,
Manage Files and Directories
mkdir
Make a directory
-p: Make a directory and also any parent directories, if they do not exist
rmdir
Remove an empty directory
cd
Change the current directory
Some special behaviour:
Without a name the new directory is the home directory.
returns the user to the previous directory.
moves up one level in the file tree.
rm
Remove a file
-r: Remove a file or a directory with all files and directories in it
-i: Get a question before taking action (removing files, removing directories, descending in directories)
mv
Change the name of a file or directory
cp
Copy a file to a new file
or to another directory
Copy all files in the current directory to another directory
-p: Copy with preserved file settings (modification date, permissions, ownership):
-r: Copy a directory and its subdirectories
-u: Copy only files that do not exist or have an earlier modification date in another directory
ls
List files in the current directory
List the files in another directory
-l: List in long format
-s: List with size (in blocks by default)
-S: List sorted by size
-t: List sorted by time
-r: Reverse the order of a sorted listing
-h: Print size in human-readable form (with units adjusted to size)
List files in long format with most recent file last
find
Find a file/directory with a given name and print its path, starting from a given directory
Find all files that contain a specified string in their name, starting from the current directory
Find files newer than a specified file, starting from the current directory
-exec ... {} \;: Execute a command with the found file represented by {}
Remove files and directories with a specified string in their name
-not: Negate an expression
-type d: Specify the file type as a directory
Get a long-format listing of files with a specified string in their name, as long as they are not directories
find is a powerful command with many options as a look at the man page will reveal,
du
Print the disk usage of the current and all subdirectories
Print the disk usage of a specifed directory and all its subdirectories
-a: Print the size of individual files in addition to diectories
Print the size of a file
-s: Just print the sum of the disk usage
--max-depth=level: Print usage for directories level levels down, where level is a number
-k: Print usage in kB
-m: Print usage in MB
-h: Print usage in human-readable form (with units adjusted after the size)
Print the summed disk usage of the current directory in human-readable form
Print the disk usage for the current directory and its immediate subdirectories in human-readable form
chmod
Set the file permission.
Make a file executable for anyone
To be more precise it is possible to specify the changeas a string in the format who-add/remove-permission, where who is one of more of the characters u (user), g (group), and o (others); + means add and - means remove; and permission is one or more of the characters r (read), w (write), and x (execute). For example, to remove read and write permissions for the group and others
-R: Make the change recursively, i.e., for all files in all subdirectories
Text Handling
cat
List the contents of a file
List the contents of several files and put the result in a new file
Append a file to another file
more
List the contents of a file without scrolling through everything at once
Display the output of a command without scrolling through everything at once
Pressing return gives a new line. Pressing the space bar gives a new page. b gives the previous page if possible (not for the output stream of a command). /string searches for a string. q exists more.
less
less is similar to more. One of the more important differences is that it also works backwards. For example, pressing b gives the previous page (works also for more on files - first more example, but not pipes - second more example) and ?string searches backwards for a string.
grep
Print lines that contain a specified string in a file
Search for and print lines containing a specified string in several files
Search for and print lines containing a specified string in all files in the current directory
Print the lines in the output of a command containing a specified string
-i: Ignore the case of the search string (print both upper and lower case matches)
-v: Print lines that do not contain the string
diff
Compare the contents of two files and print differences
--side-by-side: Display the file contents side-by-side with differences marked
Author: (LUNARC)
Last Updated: 2022-10-05