Useful Linux Commands for CS Courses #
alias #
Creates aliases – words that are replaced by a command string. Aliases expire with the current shell session unless defined in the shell’s configuration file, e.g. ~/.bashrc.
More information: help alias
Examples
List all aliases:
alias
Create a generic alias:
alias {{word}}="{{command}}"
View the command associated to a given alias:
alias {{word}}
Remove an aliased command:
unalias {{word}}
Turn rm into an interactive command:
alias {{rm}}="{{rm --interactive}}"
Create la as a shortcut for ls –all:
alias {{la}}="{{ls --all}}"
aspell #
Interactive spell checker.
More information: man aspell
Examples
Spell check a single file:
aspell check {{path/to/file}}
List misspelled words from standard input:
cat {{file}} | aspell list
List misspelled words from standard input and ignore words from personal word list:
cat {{file}} | aspell --personal={{personal-word-list.pws}} {{list}}
astyle #
Source code indenter, formatter, and beautifier for the C, C++, C# and Java programming languages. Upon running, a copy of the original file is created with an “.orig” appended to the original file name.
For full usage, please refer to the HTML documentation at http://astyle.sourceforge.net/.
More information: man astyle
Examples
Apply the default style of 4 spaces per indent and attached braces:
astyle {{source_file}}
Apply the Stroustrup style with attached braces:
astyle --style=stroustrup {{path/to/file}}
Apply the Allman style with broken braces:
astyle --style=allman {{path/to/file}}
cal #
Prints calendar information, with the current day highlighted.
More information: man cal
Examples
Display a calendar for the current month:
cal
Display previous, current and next month:
cal -3
Display a calendar for a specific year (4 digits):
cal {{year}}
Display a calendar for a specific month and year:
cal {{month}} {{year}}
cat #
Print and concatenate files.
More information: man cat
Examples
Print the contents of a file to the standard output:
cat {{file}}
Concatenate several files into the target file:
cat {{file1}} {{file2}} > {{target_file}}
Append several files into the target file:
cat {{file1}} {{file2}} >> {{target_file}}
Number all output lines:
cat -n {{file}}
Display non-printable and whitespace characters (with M- prefix if non-ASCII):
cat -v -t -e {{file}}
cd #
Change the current working directory.
More information: help cd
Examples
Go to the specified directory:
cd {{path/to/directory}}
Go up to the parent of the current directory:
cd ..
Go to the home directory of the current user:
cd
Go to the previously chosen directory:
cd -
clang++ #
Compiles C++ source files.
More information: man clang
Examples
Compile a source code file into an executable binary:
clang++ {{path/to/source_file}} -o {{path/to/output_executable}}
Display (almost) all errors and warnings:
clang++ -Wall {{path/to/source_file}} -o {{path/to/output_executable}}
Choose a language standard to compile with:
clang++ -std={{c++14}} {{path/to/source_file}} -o {{path/to/output_executable}}
clang-tidy #
An LLVM-based C/C++ linter to find style violations, bugs and security flaws through static analysis.
More information: man clang-tidy
Examples
Run default checks on a source file:
clang-tidy {{path/to/source_file}}
Don’t run any checks other than the C++ Core Guidelines (cppcoreguidelines) checks on a file:
clang-tidy {{path/to/source_file}} -checks={{-*,cppcoreguidelines-*}}
List all available checks:
clang-tidy -checks={{*}} -list-checks
clear #
Clears the terminal screen. See also: reset
More information: man clear
Example
Clear the screen (equivalent to pressing Control-L in Bash shell):
clear
cp #
Copy files and directories.
More information: man cp
Examples
Copy a file to another location:
cp {{path/to/source_file.ext}} {{path/to/target_file.ext}}
Copy a file into another directory, keeping the filename:
cp {{path/to/source_file.ext}} {{path/to/target_parent_directory}}
Recursively copy a directory’s contents to another location (if the destination exists, the directory is copied inside it):
cp -R {{path/to/source_directory}} {{path/to/target_directory}}
Copy a directory recursively, in verbose mode (shows files as they are copied):
cp -vR {{path/to/source_directory}} {{path/to/target_directory}}
Copy text files to another location, in interactive mode (prompts user before overwriting):
cp -i {{*.txt}} {{path/to/target_directory}}
Make symbolic link instead of copying:
cp -s {{path/to/source_file.ext}} {{path/to/target_file.ext}}
cppcheck #
A static analysis tool for C/C++ code.Instead of syntax errors, it focuses on the types of bugs that compilers normally do not detect.
More information: https://sourceforge.net/p/cppcheck/wiki/Home/
Examples
Check a given file, specifying which tests to perform (by default only errors are shown):
cppcheck --enable={{error|warning|style|performance|portability|information|all}} {{path/to/source_file}}
List available tests:
cppcheck --errorlist
Check a given file, ignoring specific tests:
cppcheck --suppress={{test_id1}} --suppress={{test_id2}} {{path/to/source_file}}
cpplint #
A style checker for C/C++ code following Google’s style guidelines.
More information: https://google.github.io/styleguide/cppguide.html
Examples
Check a given file for style violations:
cpplint {{path/to/source_file}}
Display help screen:
cpplint --help
Check a given file, specifying a comma-separated list of category filters to apply. Only error messages whose category names pass the filters are printed.
cpplint --filter='whitespace,+whitespace/braces {{path/to/source_file}}
date #
Display the system date.
More information: man date
Examples
Display the current date using the default locale’s format:
date
Display the current date in UTC and ISO 8601 format:
date -u +"%Y-%m-%dT%H:%M:%SZ"
Display the current date as a Unix timestamp (seconds since the Unix epoch):
date +%s
Display a specific date (represented as a Unix timestamp) using the default format:
date -d @1473305798
Convert a specific date to the Unix timestamp format:
date -d "{{2018-09-01 00:00}}" +%s --utc
diff #
Compare files and directories.
More information: man diff
Examples
Compare files (lists changes to turn old_file into new_file): diff {{old_file}} {{new_file}}
Compare files, ignoring white spaces: diff –ignore-all-space {{old_file}} {{new_file}}
Compare files, showing the differences side by side: diff –side-by-side {{old_file}} {{new_file}}
file #
Determine file type.
More information: man file
Examples
Give a description of the type of the specified file. Works fine for files with no file extension:
file {{filename}}
Look inside a zipped file and determine the file type(s) inside:
file -z {{foo.zip}}
g++ #
Compiles C++ source files.Part of GCC (GNU Compiler Collection).
More information: man g++
Compile a source code file into an executable binary:
g++ {{path/to/source.cpp}} -o {{path/to/output_executable}}
Display common warnings:
g++ {{path/to/source.cpp}} -Wall -o {{path/to/output_executable}}
Choose a language standard to compile for (C++98/C++11/C++14/C++17):
g++ {{path/to/source.cpp}} -std={{c++98|c++11|c++14|c++17}} -o {{path/to/output_executable}}
Include libraries located at a different path than the source file:
g++ {{path/to/source.cpp}} -o {{path/to/output_executable}} -I{{path/to/header}} -L{{path/to/library}} -l{{library_name}}
Compile and link multiple source code files into an executable binary:
g++ -c {{path/to/source_1.cpp path/to/source_2.cpp ...}} && g++ -o {{path/to/output_executable}} {{path/to/source_1.o path/to/source_2.o ...}}
Display version:
g++ --version
gdb #
The GNU Debugger.
More information: https://www.gnu.org/software/gdb.
Examples
Debug an executable:
gdb executable
Execute given GDB commands upon start:
gdb -ex "commands" executable
Start
gdb
and pass arguments to the executable:gdb --args executable argument1 argument2
geany #
A small and fast editor with basic features of an integrated development environment (IDE).
More information: man geany
Examples
Open geany. Automatically loads previous session’s files:
geany
Open geany. Don’t load the previous session’s files.
geany --no-session
Open a specific file:
geany {{path/to/file}}
Open a specific file, positioning the cursor at the specified line:
geany +{{line}} {{path/to/file}}
Open a specific file, positioning the cursor at the specified line and column:
geany --line {{line}} --column {{column}} {{path/to/file}}
head #
Output the first part of files. See also: tail
.
More information: man head
Examples
Output the first few lines of a file:
head --lines {{count}} {{path/to/file}}
Output the first few bytes of a file:
head --bytes {{count}} {{path/to/file}}
Output everything but the last few lines of a file:
head --lines -{{count}} {{path/to/file}}
Output everything but the last few bytes of a file:
head --bytes -{{count}} {{path/to/file}}
history #
Command-line history.
More information: man history
Examples
Display the commands history list with line numbers:
history
Display the last 20 commands:
history {{20}}
judge #
An automated program judge that executes a program and pipes the contents of an input file to the program’s standard input. It then compares the program’s standard output against an expected output.
The Judge issues one of the following verdicts: Accepted (AC), Presentation Error (PE), Wrong Answer (WA), Output Limit Error (OLE), Time Limit Error (TLE), or Run Time Error (RTE). Also see: diff
Examples
Display a help screen.
judge -h
Run the judge with specific inputs and expected outputs, reporting only the verdict:
judge -p {{program}} -i {{input}} -o {{expected_output}}
Run the judge with verbose output.
judge -v -p {{program}} -i {{input}} -o {{expected_output}}
Run the judge, enabling verbose output and logging all activities to a file. Each run generates a new {{pid}}.log file:
judge -l -p {{program}} -i {{input}} -o {{expected_output}}
Run the judge with a specific time limit (default: 5 seconds):
judge -t {{seconds}} -p {{program}} -i {{input}} -o {{expected_output}}
less #
Open a file for interactive reading, allowing scrolling and search.
More information: man less
Examples
Open a file:
less {{source_file}}
Page down / up:
<Space>
(down), b (up)Go to end / start of file: G (end), g (start)
Forward search for a string (press n/N to go to next/previous match): /{{something}}
Backward search for a string (press n/N to go to next/previous match): ?{{something}}
Exit: q
ls #
List directory contents.
More information: man ls
Examples
List files one per line:
ls -1
List all files, including hidden files:
ls -a
List all files, with trailing / added to directory names:
ls -F
Long format list (permissions, ownership, size, and modification date) of all files:
ls -la
Long format list with size displayed using human-readable units (KiB, MiB, GiB):
ls -lh
Long format list sorted by size (descending):
ls -lS
Long format list of all files, sorted by modification date (oldest first):
ls -ltr
Only list directories:
ls -d */
man #
Format and display manual pages.
More information: man man
Examples
Display the man page for a command:
man {{command}}
Display the man page for a command from section 7:
man {{7}} {{command}}
Search for manpages containing a search string:
man -k "{{search_string}}"
mkdir #
Creates a directory.
More information: man mkdir
Examples
Create a directory in current directory or given path:
mkdir {{directory}}
Create multiple directories in the current directory:
mkdir {{directory_1 directory_2 ...}}
Create directories recursively (useful for creating nested dirs):
mkdir -p {{path/to/directory}}
mv #
Move or rename files and directories.
More information: man mv
Examples
Move a file to an arbitrary location:
mv {{source}} {{target}}
Move files into another directory, keeping the filenames:
mv {{source1}} {{source2}} {{source3}} {{target_directory}}
Do not prompt for confirmation before overwriting existing files:
mv -f {{source}} {{target}}
Prompt for confirmation before overwriting existing files, regardless of file permissions:
mv -i {{source}} {{target}}
Do not overwrite existing files at the target:
mv -n {{source}} {{target}}
Move files in verbose mode, showing files after they are moved:
mv -v {{source}} {{target}}
nano #
Simple, easy to use command-line text editor. An enhanced, free Pico clone.
More information: man nano
Examples
Open a new file in nano:
nano
Open a specific file:
nano {{path/to/file}}
Open a specific file, positioning the cursor at the specified line and column:
nano +{{line}},{{column}} {{path/to/file}}
pclint #
A commercial static analysis tool for C/C++ code. Instead of syntax errors, it focuses on the types of bugs that compilers normally do not detect. It can be used for quality assurance of C/C++ source code and checking the code for conformance to coding standards (e.g., MISRA, CERT). See also: pclint-info
, cppcheck
, cpplint
.
Examples
Display a command summary:
pclint ?
Check a given file using default checks:
pclint {{path/to/source_file}}
Check a given file, disabling diagnostics by message number:
pclint -e{{message_number}} -e{{message_number}} {{path/to/source_file}}
pclint-info #
Display detailed pclint diagnostic information by number. See also: pclint
More information:
Examples
Display information about a specific diagnostic:
pclint-info {{message_number}}
Display information about multiple diagnostics, in order listed:
pclint-info {{message_number}} {{message_number}} {{message_number}}
passwd #
Passwd is a tool used to change a user’s password.
More information: man passwd
Examples
Change the password of the current user interactively:
passwd
Get the current status of the user:
passwd -S
pwd #
Print name of current/working directory.
More information: man pwd
Example
Print the current directory:
pwd
quota #
Display users’ disk space usage and allocated limits.
More information: man quota
Examples
Show disk quotas in human-readable units for the current user:
quota -s
Verbose output (also display quotas on filesystems where no storage is allocated):
quota -v
Quiet output (only display quotas on filesystems where usage is over quota):
quota -q
reset #
Reinitializes the current terminal. Clears the entire terminal screen. See also: clear
.
More information: man reset
Examples
Reinitialize the current terminal:
reset
Display the terminal type instead:
reset -q
rm #
Remove files or directories. Caution: Deleted files/directories cannot be recovered.
More information: man rm
Examples
Remove files from arbitrary locations:
rm {{path/to/file}} {{path/to/another/file}}
Recursively remove a directory and all its subdirectories:
rm -r {{path/to/directory}}
Forcibly remove a directory, without prompting for confirmation or showing error messages:
rm -rf {{path/to/directory}}
Interactively remove multiple files, with a prompt before every removal:
rm -i {{file(s)}}
Remove files in verbose mode, printing a message for each removed file:
rm -v {{path/to/directory/*}}
rmdir #
Removes a directory.
More information: man rmdir
Remove directory, provided it is empty. Use
rm -r
to remove non-empty directories:rmdir {{path/to/directory}}
Remove the target and its parent directories (useful for nested dirs):
rmdir -p {{path/to/directory}}
scp #
Secure copy. Copy files between hosts using Secure Copy Protocol over SSH.
More information: man scp
Examples
Copy a local file to a remote host:
scp {{path/to/local_file}} {{remote_username}}@{{remote_host}}:{{path/to/remote_directory}}
Copy a file from a remote host to a local directory:
scp {{remote_username}}@{{remote_host}}:{{path/to/remote_file}} {{path/to/local_directory}}
Recursively copy the contents of a directory from a remote host to a local directory:
scp -r {{remote_username}}@{{remote_host}}:{{path/to/remote_directory}} {{path/to/local_directory}}
ssh #
Secure Shell is a protocol used to securely log onto remote systems. It can be used for logging or executing commands on a remote server.
More information: man ssh
Example
Connect to a remote server using trusted X11 forwarding:
ssh -Y {{username}}@{{remote_host}}
sshfs #
Filesystem client based on SSH.
More information: man sshfs
Examples
Mount remote directory:
sshfs {{username}}@{{remote_host}}:{{remote_directory}} {{mountpoint}}
Unmount remote directory:
umount {{mountpoint}}
Use compression:
sshfs {{username}}@{{remote_host}}:{{remote_directory}} -C
tail #
Display the last part of a file. See also: head
.
More information: man tail
Examples
Show last ‘count’ lines in file:
tail --lines {{count}} {{path/to/file}}
Print a file from a specific line number:
tail --lines +{{count}} {{path/to/file}}
Print a specific count of bytes from the end of a given file:
tail --bytes {{count}} {{path/to/file}}
tree #
Show the contents of the current directory as a tree.
More information: man tree
Examples
Print files and directories up to ’num’ levels of depth (where 1 means the current directory):
tree -L {{num}}
Print directories only:
tree -d
Print hidden files too with colorization on:
tree -a -C
Print the tree without indentation lines, showing the full path instead (use -N to not escape non-printable characters):
tree -i -f
Print the size of each file and the cumulative size of each directory, in human-readable format:
tree -s -h --du
Print files within the tree hierarchy, using a wildcard (glob) pattern, and pruning out directories that don’t contain matching files:
tree -P '{{*.txt}}' --prune
turnin #
Submit assignment for grading.
More information: turnin --help
Examples
Turn in a single file:
turnin --class {{class}} --project {{project}} --verbose {{source_file}}
Turn in multiple files:
turnin --class {{class}} --project {{project}} --verbose {{path/to/file1 path/to/file2 path/to/file3}}
List projects for a course.
turnin --class {{class}} --list
valgrind #
Wrapper for a set of expert tools for profiling, optimizing and debugging programs. Commonly used tools include memcheck, cachegrind, callgrind, massif, helgrind, and drd.
More information: man valgrind
Examples
Use the (default) Memcheck tool to show a diagnostic of memory usage by program:
valgrind {{program}}
Use the (default) Memcheck tool to report all possible memory leaks of program in full detail:
valgrind --leak-check=full --show-leak-kinds=all {{program}}
Use the Massif tool to profile and log heap memory and stack usage of program:
valgrind --tool=massif --stacks=yes {{program}}
wc #
Count lines, words, and bytes.
More information: man wc
Examples
Count all lines, words, and bytes in a file:
wc {{path/to/file}}
Count all lines in a file:
wc --lines {{path/to/file}}
Count all words in a file:
wc --words {{path/to/file}}
Count all bytes in a file:
wc --bytes {{path/to/file}}