Linux Commands
history example
$ *history* 1 clear 2 ls -al 3 sudo dnf update -y Now just type !3 to rerun the command on line 3
cd command
*Change Directory* Let's say you're in */home/username/Documents* and you want to go to Photos, a subdirectory of Documents. To do so, simply type the following command: *cd Photos.* Another scenario is if you want to switch to a completely new directory, for example, */home/username/Movies*. In this case, you have to type *cd* followed by the directory's absolute path: *cd /home/username/Movies*
cat command
*Concatenate* Lists the contents of a file on the standard output (sdout). To run this command, type *cat* followed by the file's name and its extension. For instance: *cat file.txt*
pwd command
*Print Working Directory* Finds out the path of the current working directory (folder) you're in. The command will return an absolute (full) path, which is basically a path of all the directories that starts with a forward slash (/). An example of an absolute path is */home/username*
sudo command
*SuperUser Do* Enables you to perform tasks that require administrative or root permissions. Easy for an error to occur if you do something wrong.
cat > filename.txt
*cat > filename* creates a new file with the name filename.txt. Type text and then use 'Ctrl-d' at the end to save the file.
cat filename1.txt filename2.txt>filename3.txt
*cat filename1.txt filename2.txt>filename3.txt* joins two files (1 and 2) and stores the output of them in a new file (3)
cd
*cd* goes straight to the home folder
cd-
*cd-* (with a hyphen) to move to your previous directory
cd..
*cd..* (with two dots) to move one directory up
chmod command
*change mode* Changes the read,write, & execute permissions of files/directories.
cp command
*copy* Copy files from the current directory to a different directory. For instance, the command *cp scenery.jpg /home/username/Pictures* would create a copy of *scenery.jpg* (from your current directory) into the *Pictures* directory.
diff command
*difference* Compares files line by line & will output any lines that do not match. Used to make program alternations instead of rewriting the entire source code. *diff file1.ext file2.ext*
df command
*disk free* Gives report on system's disk space usage, shown in % and KBs. If you want to see the report in MB, type *df -m*
du command
*disk usage* Checks how much space a file/directory takes. Shows disk block numbers, not usual size format. To see file size, add the *-h* argument.
grep command
*global regular expression print* Lets you search through all the text in a given file. Ex: *grep blue notepad.txt* will search for the word blue in the *notepad.txt* file. Lines that contain the searched word will be displayed fully.
ls command
*list* Used to list the contents of a directory. If you want to see the content of other directories, type *ls* and then the directory's path. For example, enter *ls /home/username/Documents* to view the content of Documents.
ls -R
*ls -R* will list all the files in the sub-directories as well
ls -a
*ls -a* will show the hidden files
ls -al
*ls -al* will list the files and directories with detailed information like the permissions, size, owner, etc.
mkdir command
*make directory* If you type *mkdir Music* it will create a directory called *Music*
man command
*manual* Shows manual for program. Typing *man tail* will show the manual instruction of the *tail* command.
mv command
*move* Moves & renames files. Type *mv*, the file's name, and the destination's directory. For example: *mv file.txt /home/username/Documents.* To rename, type *mv oldname.ext newname.ext*
rmdir command
*remove directory* Allows you to delete empty directories.
rm command
*remove* Allows you to delete directories and contents within them.
tar command
*tarball* Archives files into a *tarball*
zip/unzip command
*zip* compresses your files into a zip archive and *unzip* extracts the zipped files from a zip archive
find variations
-To find files in the current directory use: *find . -name notes.txt* -To look for directories use: */ -type d -name notes.txt*
echo command
Adds data into a file. To add "Hello John" into a file, you would type *echo Hello John >> name.txt*
clear command
Blanks the terminal screen erasing previously displayed output
;
Can run multiple commands in a single command by using *;* to separate them. *command1; command2*
ping command
Checks connectivity status. By simply entering *ping google.com*, the command will check whether you're able to connect to Google and also measure the response time.
Tarball
Common Linux file format similar to zip format, with compression optional
touch command
Creates a blank new file through the Linux command line. As an example, enter *touch /home/username/Documents/Web.html* to create an HTML file entitled *Web* under the *Documents* directory.
jobs command
Displays all current jobs along with their statuses. A job is basically a process that is started by the shell.
head command
Displays first 10 lines of text file. To show 5 lines use: *head -n 5 filename.ext"
tail command
Displays last 10 lines of text file. *tail -n filename.ext*
SIGKILL (9)
Forces program to stop immediately. Unsaved progress is lost
hostname command
If you want to know the name of your host/network simply type *hostname*. Adding a *-I* to the end will display the IP address of your network.
history command
Keeps a list of all the other commands that have been run from that terminal session, then allows you to replay or reuse those commands instead of retyping them.
&&
Like *;* but only runs next command if the previous is successful. *command1 && command2*
Ctrl+A
Moves you to beginning of line
Ctrl+E
Moves you to end of line
Process Identification Number (PID)
PID of program is needed to kill it. If you don't know the PID, use the *ps ux* command.
Ctrl+S
Pause all command output to the screen.
kill command
Sends signal to app that instructs the app to terminate itself. Total of sixty-four signals, but mainly 2 are used. *kill [signal option] PID*
find command
Similar to the locate command, using find also searches for files and directories. The difference is, you use the find command to locate files within a given directory. Example: *find /home/ -name notes.txt* command will search for a file called *notes.txt* within the home directory and its subdirectories.
top command
Terminal equivalent to Task Manager. Displays list of running processes and how much CPU each uses.
locate -i
The *-i* argument makes the search case insensitive
TAB
The TAB button can autofill what you are typing. EX: type *cd Docu* then hit TAB and the terminal will fill in the rest, showing you *cd Documents*
locate command
This command locates a file, just like the search command in Windows. Case sensitive by default.
useradd & passwd example
To add new person named John: *useradd John* Then to add his password: *passwd 12345*
cat filename | tr a-z A-Z >output.txt
To convert a file to upper or lower case use, *cat filename.txt | tr a-z A-Z >output.txt*
mkdir variations
To generate a new directory inside another directory, use this Linux basic command *mkdir Music/Newfile* use the *p* (parents) option to create a directory in between two existing directories. For example, *mkdir -p Music/2020/Newfile* will create the new "2020" file.
locate (*)
To search for a file that contains two or more words, use an asterisk (*). For example, *locate* *-i* *school***note* command will search for any file that contains the word "school" and "note", whether it is uppercase or lowercase.
Ctrl+Q
Turns all output stopped on-screen back on (XON). Also, closes an application window.
passwd
Used to add a password to a user's account
useradd/userdel command
Used to add or delete users from a linux system.
Ctrl+Z
Will pause the command.
Ctrl+C
Will stop & terminate the command.
wget command
command-line utility tool used to download files from the internet. Simply type *wget* followed by download link.
SIGTERM (15)
kill signal that requests a program to stop running and gives it some time to save all of its progress. If you don't specify signal when entering kill command, this is the default signal.
uname command
uname prints detailed info about your Linux system like machine name, OS, kernel, etc.