Linux Command Lines

Linux Command Lines

If you're considering using Linux... No, you are not considering using Linux. You are using Linux if you consider starting a career in cybersecurity and learning basic commands are as important as the course itself.

It's ok, don't be scared.

The Linux command lines are the first set of information you need to digest before you dive into the course proper. This gives you a better understanding of how the terminal works and how you can navigate on the terminal.

Welcome📣, to the magical class of Command-Line interface, and this my friend, is where you sit back, relax and learn.

Let's define, Linux command line:

The Linux command line is a text-based interface that passes commands to the computer. This text-based interface could be a shell, terminal, console among others.

cat (concatenate)

Prints one or more files contents to the standard output device (usually your monitor). cat [filename] such as cat newfile.txt

head

This command displays the first n lines of a file, n is 10 by default. The number of lines can be specified using the "-n" option, and "-c" can be used to specify the number of bytes to display instead of lines. Example head -5 newfile.txt.

tail

Display the last n lines of a file (the default is 10). The number of lines can be specified using the "-n" option, and "-c" can be used to specify the number of bytes to display instead of lines. "tail" also has a "follow" option ("-F") which outputs new data as it is appended to the end of a file. This is convenient for watching log files in real-time. Example tail newfile.txt.

cd (change directory)

Changes the current working directory to a specified one or a relative path. Example cd (/directorypath), cd /home/Documents .

chmod (change mode)

Change a file's permissions. Example chmod +g r-x to change group permission to read and execute a file or directory.

chown (change owner)

Change who owns a file.

clear

Clear a command-line screen/window for a fresh start.

cp (copy)

Copy files and directories. To copy multiple files at once, specify each as a parameter or use a wildcard like this*.txt. To copy a directory and its contents -R (recursive) is added to cp command. Example cp -R newfile1/ newfile2/

date

Display or set the system date and time.

df

Displays the amount of disk space that has been used and the amount of disk space that is still available.

du

Show how much space each file takes up.

file

Determine what type of data is within a file. Example file [option] [filename] file newscript.py, file -b newfile.txt.

find

Search for files matching a provided pattern. It keeps a database of "magic numbers" that correspond to different file types.

grep (global regular expression print)

Search files or output for a particular pattern. It is used as part of a pipeline instead of directly on a file. As in netstat -nat | grep ESTABLISHED

kill

The kill command sends a signal to a process that causes it to terminate. If the process refuses to stop, the kill -9 command is used. Use man kill to get more references about the "kill" command.

less

View a file's contents one page at a time, making it easier to read. The less command is commonly used to pipe the output of commands into, particularly those commands with a lot of output. less [option] filename as in less -n newfile.txt.

Create a shortcut to a file. To create a symbolic link, use the "-s" option. Example ln -s newfile.txt my_link.txt. Before being added to the command, my_link.txt must be created.

locate

Search a copy of your file by a specified name. Example locate newfile.txt.

lpr

Send a print job. Example lpr my_file.txt.

ls (list directory contents)

List files in a directory. ls -al list all contents of a directory including hidden files.

lsof(list open directory)

Displays a list of the system's open files. The list contains information about which user and which process has the file open. "lsof" also has a "-i" option that will display a list of programs that are listening for network traffic, similar to the output of netstat -na | grep LISTEN

netstat

Displays information about the system's TCP and UDP connections, including established connections and ports with services listening for incoming connections. The "-t" option can be used to show only TCP and "-u" options can be used to show only UDP information. Example netstat -lt to list all listening ports.

man (manual)

Display the help information for the specified command. Example man nmap displays reference manuals on nmap.

mkdir (make directory)

Create a new directory. To create any parent directory the -p option is added to the 'mkdir' command. As in mkdir -p /Green1/Green2/Green3 will create "/Green1" if such does not already exist, then create "/Green1/Green2" if such does not already exist, and finally create "/Green1/Green2/Green3" if such does not already exist. Please note that If any of them exist, no errors are returned.

ifconfig (interface config)

Displays network interface information such as your IP address. (similar to "ipconfig" on Windows). ifconfig can also be used to set the IP address for an interface.

mv (move)

Rename or move file(s) or directories. Like "cp" you can move multiple files by specifying each one individually or with a wildcard, followed by the destination directory as the final parameter.

mv -i: interactive prompt before overwrite

mv -u: update - move when the source is newer than the destination

mv -v: verbose - print source and destination files

man mv: help manual

passwd(change password)

Change the password or allow (the system administrator) to change any users password.

ps (processes status)

Displays a snapshot of the processes that are currently active. The "aux" options specify that all running processes in the system, rather than just those from the current shell, should be listed. Output from "ps" is often piped into 'grep' to search for instances of a particular program

pwd (print working directory)

Display the pathname for the current directory.

rm (remove)

Remove (delete) file(s) and/or directories. Example rm newfile.txt

rmdir (remove directory)

Delete empty directories. To delete a directory and its contents use -r (recursive), as in rmdir -r. It's also worth noting that directories or files deleted with the 'rmdir' and "rm" command, as well as directories and their contents deleted with the 'rmdir -r' command, cannot be recovered.

ssh (secure shell)

Log in to another Linux machine remotely via the network. To leave an ssh session type "exit". Format ssh [ip address].

su (substitute user)

Temporarily switch to another user account (most commonly root). The "-" option makes the shell a login shell, causing it to inherit the target user's environment

stat (statistics)

Shows some statistics about a file, including its name, size, last modified date, and permissions. Example stat /etc/resolv.conf.

ping

Sends ICMP ECHO_REQUEST packets to a network host and waits for responses to test network connectivity. Format: ping [ip address], Example ping 8.8.8.8. The "ping6" program can be used for IPv6 addresses.

top

Displays the resources being used on your system. Press q to exit.

touch

Create an empty file with the specified name. Example touch hello.txt.

uniq (unique)

Removes duplicate lines from a sorted file. Format uniq [option] [input[output]].

who

Display who is logged on.

whoami

Display the current username

Special Characters

/ (forward slash)

It is used between directory names as a separator. Example cd /home/Documents

\ (backslash)

Escape character; it is used to reference other special characters. Example touch myfile.txt\

. (single dot)

Represents current directory. It is also used as the first character for a hidden file or directory. Example ls ./file.

.. (two dots)

represents parent directory usually one level up from the current directory. Example cd ..

~(tilde)

It represents the current user's home directory. This can also be used as an abbreviation for it. Example cd ~/Destop as a shortcut for cd /home/username/Desktop.

& (ampersand)

This character is used to execute a command in the background as a job. When you run the command in the background, you get a shell prompt immediately rather than having to wait for it to finish. Example gedit &

* (astericks)

This wildcard is used to represent zero or more characters in a filename. Example ls *.txt will list out all files with the ending '.txt' such as 'newfile.txt', and 'newdoc.txt'.

? (question mark)

This wildcard is used to represent a single character in a file name. Example ls newfile?.txt.

[ ] (square bracketss)

These are used to specify the range of values to match. Example [0-9] will match digits 0 through 9, [A-Z] will match uppercase letters.

; (semi colon)

This is a command separator that can be used to run multiple commands on a single line unconditionally. Format cmd1 ;cmd2.

&& (double ampersand)

This is a command separator and will run only the second command if the first command is successful or does not return an error. It is commonly used in shell scripts. Format cmd1 && cmd2.

|| (double pipe)

This is a command separator that will only run the second command if the first command fails or has errors. Also used in shell scripts. Format cmd1 || cmd2.

Archiving and compressing tools

These are commands that are used to combine multiple files into a single package (archive) or to reduce the size of a file (compress). Here are some commands to help you along the way.

bzip2

Compresses files into .bz2 format. Used mostly for incredibly large sets of text files (which is what source code actually is).

bunzip2

Uncompresses .bz2 files.

compress

Compresses files into .z format. Pretty old and not used much in the Linux world.

gunzip

Uncompresses .gz files and .tgz files.

gzip

Compresses files into .gz format.

tar

Packages files together in a group. The most common way of using this command is tar xvf filename, Example tar xvf download.tar.

uncompress

Uncompresses files from .z format.

unzip

Uncompresses files from `.zip format

Extra! Extra!!

Kernel Support Commands

depmod : Regenerates your module dependencies.

insmod : Loads a module by hand.

lsmod : Lists the modules your kernel has loaded.

modprobe : Loads a module by hand along with its dependencies and settings.

rmmod : Unloads a module by hand