Linux Basics
Syntax
- Note that Linux command are case sensitive.
- The hash (pound) sign # indicates end of a command and the start of a comment.
- When specifying file names:
- The
.(dot) refers to the present working directory - The
..(dot dot) refers to a folder one up from the present working directory - The
~(tilde) refers to the user’s home directory
- The
Keyboard shortcut
Ctrl key can be replace with Command on Mac keyboard.
| Bash Navigation | |
|---|---|
| Ctrl + A | Move to the start of the command line |
| Ctrl + E | Move to the end of the command line |
| Bash Control/Process | |
|---|---|
| Ctrl + L | Clears the terminal screen |
| Ctrl + Z | Suspends current command execution and moves it to the background |
| Ctrl + Q | Resumes suspended command |
| Ctrl + C | Sends SIGI signal and kills currently executing command |
| Bash History | |
|---|---|
| Ctrl + R | Incremental reverse search of bash history |
| Up arrow | Moves to previous command |
| Down arrow | Moves to next command |
| Bash Information | |
|---|---|
| TAB | Autocompletes the command or file/directory name |
Command
File commands
#Print the current directory
pwd
#List contents of current directory
ls
#List files one per line
ls -1
#List all files, including hidden files
ls -a
#Long format list (permissions, ownership, size, and modification date)
ls -l
#Long format list (permissions, ow nership, size, and modification date) of all files including hidden files and folders
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 */
#List files and directories in user home directory
ls ~
#Go to the home directory of the current user
cd
#Go to the home directory of the current user
cd ~
#Go to the specified directory
cd {{path/to/directory}}
#Go up to the parent of the current directory
cd ..
#Go up to 2 directory up from the current directory
cd ../..
#Go to the previously chosen directory
cd -
#Create specific directories
mkdir {{path/to/directory1 path/to/directory2 ...}}
#Create specific directories and their [p]arents if needed
mkdir -p {{path/to/directory1 path/to/directory2 ...}}
#Remove specific files
rm {{path/to/file1 path/to/file2 ...}}
#Remove specific files ignoring nonexistent ones and without confirmation prompt
rm -f {{path/to/file1 path/to/file2 ...}}
#Remove specific files printing info about each removal
rm -v {{path/to/file1 path/to/file2 ...}}
#Remove specific files and directories [r]ecursively
rm -r {{path/to/file_or_directory1 path/to/file_or_directory2 ...}}
#Remove specific files and directories [r]ecursively without confirmation prompt (very dangerous)
rm -rf {{path/to/file_or_directory1 path/to/file_or_directory2 ...}}
#Remove specific empty directories
rmdir {{path/to/directory1 path/to/directory2 ...}}
#Remove specific nested empty directories recursively
rmdir -p {{path/to/directory1 path/to/directory2 ...}}
#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}}
#Rename a file or directory when the target is not an existing directory
mv {{source}} {{target}}
#Move a file or directory into an existing directory
mv {{source}} {{existing_directory}}
#Move multiple files into an existing directory, keeping the filenames unchanged
mv {{source1}} {{source2}} {{source3}} {{existing_directory}}
#Do not prompt for confirmation before overwriting existing files
mv -f {{source}} {{target}}
#Move files in verbose mode, showing files after they are moved
mv -v {{source}} {{target}}
#List the sizes of a directory and any subdirectories, in human-readable form (i.e. auto-selecting the appropriate unit for each size):
du -h {{path/to/directory}}
#Show the size of a single directory, in human-readable units:
du -sh {{path/to/directory}}
#List the human-readable sizes of a directory and of all the files and directories within it:
du -ah {{path/to/directory}}
#List the human-readable sizes of a directory and any subdirectories, up to N levels deep:
du -h --max-depth=N {{path/to/directory}}
#List the human-readable size of all .jpg files in subdirectories of the current directory, and show a cumulative total at the end:
du -ch {{*/*.jpg}}
#Create a symbolic link (shortcut) to a file or directory
ln -s {{/path/to/file_or_directory}} {{path/to/symlink}}
#Create empty files
touch {{path/to/file1 path/to/file2 ...}}
#Print the contents of a file to the standard output
cat {{file}}
#Open a file
less {{file}}
#Page down / up
<Space> (down), b (up)
#Go to end / start of file
G (end), g (start)
#Exit
q
#Output the first 10 lines of a file
head {{file}}
#Output the last 10 lines of a file
tail {{file}}
#Print the last 10 lines of a given file and keep reading file until Ctrl + C
tail -f {{file}}
Informative
file {{file}} # Give a description (text, binay, compressed, etc...) of the type of the specified file
history # Display the commands history list with line numbers
id # Display current user's ID (UID), group ID (GID) and groups to which they belong
groups # Print group memberships for the current user
Help
The command above is just the most comment command and argument for it.
If you want more detail about the program, you can use one of the following:
help {{command}} # Show help for a bash command
man {{command}} # Show the manual page for a program (press 'q' key to exit)
{{command}} --help # Show help documentation for command
{{command}} -h # Show help documentation for command
Finding things
Find files
#Find files by extension
find {{path}} -name '{{*.ext}}'
#Find directories matching a given name, in case-insensitive mode
find {{path}} -type d -iname '{{*lib*}}'
#Find files matching a given pattern, excluding specific paths
find {{path}} -name '{{*.py}}' -not -path '{{*/site-packages/*}}'
#Find files matching a given size range, limiting the recursive depth to "1"
find {{path}} -maxdepth 1 -size {{+500k}} -size {{-10M}}
#Run a command for each file (use {} within the command to access the filename)
find {{path}} -name '{{*.ext}}' -exec {{wc -l {} }}\;
#Find files modified in the last 7 days
find {{path}} -daystart -mtime -{{7}}
#Find empty (0 byte) files and delete them
find {{path}} -type {{f}} -empty -delete
Find text
#Search for a pattern within a file
grep "{{search_pattern}}" {{path/to/file}}
#Search for an exact string (disables regular expressions)
grep --fixed-strings "{{exact_string}}" {{path/to/file}}
#Use extended regular expressions (supports ?, +, {}, () and |), in case-insensitive mode:
grep --extended-regexp --ignore-case "{{search_pattern}}" {{path/to/file}}
#Print file name and line number for each match with color output:
grep --with-filename --line-number --color=always "{{search_pattern}}" {{path/to/file}}
#Search for lines matching a pattern, printing only the matched text:
grep --only-matching "{{search_pattern}}" {{path/to/file}}
Permissions and Ownership
Introduction
On a Linux system, each file and directory is assigned access rights for the owner of the file, the members of a group of related users, and everybody else. Rights can be assigned to read a file, to write a file, and to execute a file (i.e., run the file as a program).
Permissions classes
Permissions on Linux systems are split into three classes:
- User (u)
- Group (g)
- Other (o)
Files and directories are owned by a user.
Files and directories are also assigned to a group.
If a user is not the owner, nor a member of the group, then they are classified as other.
Permissions notations
In order to change permissions, we need to first understand the two notations of permissions.
- Symbolic notation
- Octal notation
Symbolic Notation
In symbolic notation, r w and x are used to show or set permission of a file or folder.
The symbolic notation are divided into 3 sets, owner, group, and other.
Read (r): The read permission allows you to open and read the content of a file. But you can't do any editing or modification in the file.Write (w): The write permission allows you to edit, remove or rename a file. For instance, if a file is present in a directory, and write permission is set on the file but not on the directory, then you can edit the content of the file but can't remove, or rename it.Execute (x): In Unix type system, you can't run or execute a program unless execute permission is set.But in Windows, there is no such permission available.
| Permission | On a File | On a directory |
|---|---|---|
| r (read) | read file content (cat) | read directory content (ls) |
| w (write) | change file content (vi) | create file in directory (touch) |
| x (execute) | execute the file | enter the directory (cd) |
Octal notation
The numeric notation is using number to instead of rwx.
| Octal | Symbol | Permissions |
|---|---|---|
| 0 | --- | None |
| 1 | --x | Execute only |
| 2 | -w- | Write only |
| 3 | -wx | Execute and Write |
| 4 | r-- | Read only |
| 5 | r-x | Read and Execute |
| 6 | rw- | Read and Write |
| 7 | rwx | Read, Write, and Execute |
Common permission
| Symbolic | Octol | Meaning |
|---|---|---|
| ugo=rwx | 777 | (rwxrwxrwx) No restrictions on permissions. Anybody may list files, create new files in the directory and delete files in the directory. Generally not a good setting. |
| u=rwx,g=rx,o=rx | 755 | (rwxr-xr-x) The directory owner has full access. All others may list the directory, but cannot create files nor delete them. This setting is common for directories that you wish to share with other users. |
| u=rwx,g=,o= | 700 | (rwx------) The directory owner has full access. Nobody else has any rights. This setting is useful for directories that only the owner may use and must be kept private from others. |
| ugo=rw | 666 | (rw-rw-rw-) All users may read and write the file. |
| u=rw,g=r,o=r | 644 | (rw-r--r--) The owner may read and write a file, while all others may only read the file. A common setting for data files that everybody may read, but only the owner may change. |
| u=rx,g=,o= | 600 | (rw-------) The owner may read and write a file. All others have no rights. A common setting for data files that the owner wants to keep private. |
Changing Permission
User and group
#Change the owner user of a file/directory
chown {{user}} {{path/to/file_or_directory}}
#Change the owner group of a file/directory
chown :{{group}} {{path/to/file_or_directory}}
#Change the owner user and group of a file/directory
chown {{user}}:{{group}} {{path/to/file_or_directory}}
#Recursively change the owner of a directory and its contents
chown -R {{user}} {{path/to/directory}}
Files and folders permission
The octal form of chmod requires that you specify user, group, and other permission.
If you need to change a single group permission, such as changing only permission for the user, group, or other, it is better to use the symbolic form.
Symbolic
#Give the [u]ser who owns a file the right to e[x]ecute it
chmod u+x {{path/to/file}}
#Give the [u]ser rights to [r]ead and [w]rite to a file/directory
chmod u+rw {{path/to/file_or_directory}}
#Remove e[x]ecutable rights from the [g]roup
chmod g-x {{path/to/file}}
#Give [a]ll users rights to [r]ead and e[x]ecute
chmod a+rx {{path/to/file}}
#Give [o]thers (not in the file owner's group) the same rights as the [g]roup
chmod o=g {{path/to/file}}
#Remove all rights from [o]thers:
chmod o= {{path/to/file}}
#Change permissions recursively giving [g]roup and [o]thers the ability to [w]rite
chmod -R g+w,o+w {{path/to/directory}}
Octal
#Give the user who owns a file the right to read and write, group and other have read access only
chmod 644 {{path/to/file}}
#Give the user who owns a file the right to read and write
chmod 600 {{path/to/file}}
#Change permissions recursively giving user read and write
chmod -R 600 {{path/to/directory}}
#Give the user full a directory, and allow everyone to view what file is in the directory
chmod 755 {{path/to/directory}}