Linux Command Line (Shell) Basics
Fast Shortcut
Ctrl + Alt + T
= Open terminal
Ctrl + C
= Kill Current Program
Crtl + Shift + C
= Copy in terminal
Crtl + Shift + V
= Paste in terminal
Tab
= Auto-complete in terminal
Note when you press enter, as well as being executed, your text is saved in a command history. Use arrow keys to browse this history to avoid retyping commands.
Basics
When the shell is ready to receive instructions from you, it will display a prompt and a cursor.
Open your linux terminal (The shell) and you will see this.
1 | user@hostname:~$ |
Hello World
1 | echo "Hello" |
$ represents your shell’s prompt.
$ at the beginning of the line is not something you need to type.
This command prints hello.
1 | echo $USER |
This command prints the user name.
1 | hostname |
This command prints the hostname.
Exit
1 | exit |
This command close the shell.
You can also press ^D (Control + D) to close the shell.
Looking around directories
Linux use / (whereas Windowas uses \ ) to separte components in the description of a file’s location.
Listing
1 | ls |
This command will list all the files in your currect directory (location).
1 | ls -a |
You can even force the shell to show you hidden files by adding -a (stand for “all”) to the command.
Move your location
1 | cd /etc |
Now you just moved from Home to a directory called “etc”.
Where am I?
print name of current/working directory.
1 | pwd |
You can use this command to print the working directory.
Wildcards of finding files
You can get a focused list by indicating the patterns of names.
1 | ls hos* |
This command list all filenames in the working directory which begin with “hos”.
You can also add * in the middle.
1 | ls c*.conf g*.conf |
This command list all filenames in the working directory which begin with c or g and end in .conf. The * represents any number of messing characters.
so ls * does the same thing as ls.
To represent exactly one character, use ‘?’.
1 | ls ???.conf |
This command will show all .conf files with 8(3+.conf) characters in their names.
Back to Home
Go back to home directory
1 | cd |
you could also use:
1 | cd ~ |
Listing files that not in your working directory
Example:
1 | ls /etc |
Remove files
1 | rmdir <yourfile> |
or
1 | rm -rf <yourfile> |
-r means “recursive” delete directories and contents.
-f means “force” dont ask any questions.
Renaming things
1 | mv <filenname> <newfilenname> |
Looking for things
Search file name
1 | find . - name services |
This command search a name called “service” in currect directory.
Search file content
1 | grep play /etc/services |
This command will output all the lines of /etc/services which contain “play”.
Cleaning up
For example, if we want to remove the “EEE” directory and everything under it, we can use this command:
be careful use this because linux has no undelete command.
1 | rm -rf EEE |
-r means “recursive” delete directories and contents.
-f means “force” dont ask any questions.
Permissions
The sudo command is required when performing actions that require root or superuser permissions, such as changing the password for another user.
1 | sudo [commend] |
Shutdown
1 | shutdown now |
You can even schedule a shutdown.
1 | shutdown 23:00 Shutdown tonight at 23:00, save your work and log out before then! |
If you dont put anything after “shutdown”, it will shutdown in a minute.
1 | shutdown |
Show Folder
1 | nautilus . |
Advanced
Ubuntu remove system error
1 | ls -l /var/crash/ |
Edit read-only file in /etc
1 | sudo -H gedit <path to file> |
Create a file from terminal
Method 1:
1 | touch <filename> |
Method 2:
1 | cat <filename> |
The file is created, but it’s empty and still waiting for the input from the user. You can type any text into the terminal, and once done CTRL-D will close it, or CTRL-C will escape you out.
Method 3:
1 | > <filename> |
Read file text
1 | cat <filename> |
Alternatively:
1 | $ less <filename> |
Print shared library dependencies
1 | ldd <filename> |
List contents of directories in a tree-like format
1 | tree -L 2 |
Set file as executable
1 | chmod +x <filename> |
Check How does thing works like a boss!
tldr : The command replacing man
TLDR: Linux Man Pages Simplified
The tldr pages are a community effort to simplify the beloved man pages with practical examples.
Script
1 | script |
A typescript will be generated. It will jot down all the command you have typed until you exit.
For more information
Common Parameters
-v, --verbose
Output a diagnostic for every file processed
-c, --changes
like verbose but report only when a change is made
-f, --silent, --quiet
suppress most error messages
–help
display this help and exit
–version
output version information and exit