Linux notes
Table of Contents
1 Files and Directories
1.1 Basic commands
ls
list filesls -l
long list with detailsls -a
show all files (including hidden ones)pwd
present working directorycd
change directory
1.2 Navigating directories
pushd
Push the current directory into a stack so it can be popped later and cd into the given directorypopd
Pop the top directory from the stack and cd into itcd -
cd into the last directory that was accessed usingcd
. Useful for alternating between two folders
1.3 Type of files
Linux doesn't need file extension to correctly identify the type of file. You can see the type of a file that doesn't have an extension using the file
command.
file translations.json translations.json: UTF-8 Unicode text, with very long lines
1.4 Locating files and commands
Use the locate
command when you know the name of a file, but are unsure about which directory it resides in. Make sure to run sudo updatedb
, so that all the files are indexed.
The whereis
command locates all the binaries of a command. Useful to know where multiple versions are installed.
The which
command is used to locate a command and prints the full path of the command being executed. This is especially useful if you have different versions of the same command installed, and want to know which version is currently active. You can then maybe change your $PATH
variable to make sure the right version gets picked up.
1.5 Find files
Use the find
command to find directories and files within a directory. Some interesting flags:
-type f
Only find files-type d
Only find directories-name <pattern>
find files which match the pattern-iname <pattern>
same a above, but ignore case-size +100K
Only files above 100 kilobytes-not
find files that don't match the given criteria-maxdepth <num>
Maximum depth of directories to recurse under-exec <command> {} +
Execute the given command on the found files
1.6 Some quick file related commands
touch <filename>
to create an empty file.
cat <filename>
to display all the contents of a file.
cat >> <filename>
to read input from stdin and append contents into <filename>. Use <C-d> to end input. Use cat > <filename>
to replace contents
less <filename>
read contents of a file in a more convenient form than cat
. Has paging and searching capabilities.
2 Help and manual pages
The whatis
command gives a short description of the command
$ whatis top top (1) - display Linux processes
The apropos
command takes an input string and displays all commands that might be related to the input string.
$ apropos editor ed (1) - line-oriented text editor editor (1) - Nano's ANOther editor, an enhanced free Pico clone editres (1) - a dynamic resource editor for X Toolkit applications ex (1) - Vi IMproved, a programmer's text editor nano (1) - Nano's ANOther editor, an enhanced free Pico clone pico (1) - Nano's ANOther editor, an enhanced free Pico clone red (1) - line-oriented text editor rview (1) - Vi IMproved, a programmer's text editor rvim (1) - Vi IMproved, a programmer's text editor sed (1) - stream editor for filtering and transforming text select-editor (1) - select your default sensible-editor from all installed editors sensible-editor (1) - sensible editing, paging, and web browsing tc-pedit (8) - generic packet editor action tc-skbmod (8) - user-friendly packet editor action vi (1) - Vi IMproved, a programmer's text editor view (1) - Vi IMproved, a programmer's text editor vim (1) - Vi IMproved, a programmer's text editor
The man
command displays the entire manual for that command if one exists
3 Package managers
3.1 apt
vs apt-get
apt
is a more simplified version of apt-get. A lot of the apt-get
commands have a corresponding command with apt
, but this is not always the case. For most regular use cases, apt
is sufficient. This specifically applies to Ubuntu distributions.
Command | Function |
---|---|
apt install | Installs a package |
apt remove | Removes a package |
apt purge | Removes package with configuration |
apt update | Refreshes repository index |
apt upgrade | Upgrades all upgradable packages |
apt autoremove | Removes unwanted packages |
apt full -upgrade | Upgrades packages with auto-handling of dependencies |
apt search | Searches for the program |
apt show | Shows package details |
3.2 Installing using dpkg
If you have a .deb file (Debian package), you can use the following command to install
sudo dpkg -i /path/to/file.deb
4 Processes and services
4.1 Some basic commands related to processes
top
Displays linux processes
htop
Interactive process viewer
ps -ef | grep <pattern>
Useful for listing processes that match the given pattern
pgrep <pattern>
Only lists the process IDs for the processes that match the given pattern. A convenient alternative to ps -ef | grep <pattern>
if you only need PIDs
4.2 Services
Services are a certain kind of process that runs in the background
sudo service <servicename> start
To start a service. Use stop
or restart
instead of start
to do what it says
5 Disk usage
du
is the command used to find disk usage. Common usages:
du -sh
Get total size of current directory
du -Sh *
Get total size of each directory and subdirectory in current directory
du -sh *
Get total size of each file/directory in the current directory
du -sh * | sort -rh | head -n 10
Get total size of each file/directory, sort in reverse and using human-readable format, display top 10 results
6 Scheduling tasks
Run crontab -e
to configure cron jobs
Check out https://crontab.guru/ to get a visual interactive feel for the cron format. Some examples:
0 5 * * 1 tar -czf /var/backups/home.tgz /home #Make a backup of home directory "At 5:00 on Monday" 0 0-23 * * * ls -la ~/Downloads > ~/Desktop/downloads.txt #Runs the given command every hour
7 Readline tips
Command | Action |
---|---|
C-h | Delete previous character |
C-w | Delete previous word |
C-u | Delete till start of command |
C-a | Jump to beginning of line |
C-e | Jump to end of line |
M-t | Swap last two words |
C-t | Swap last two letters |
C-M-y | Pull in first argument of the previous command |
M-2 C-M-y | Pull in second argument of the previous command |
C-y | Paste in the last thing that was deleted |
8 Misc
su <user>
switches user
users
lists users on the machine
id
Spits out some user related ids
watch <command>
runs a command every 2 seconds
free -h
displays amount of free and used memory in the system
chown
to change ownership of a file. Usage: sudo chown <user>:<group> <filename>
tee <filename>
Used after a pipe. Writes the output of the command before pipe into both the filename and stdout. Ex: echo "hello" | tee file.txt
. script.sh
is equivalent to source script.sh
, i.e., the dot command and the source command are equivalent. Sourcing a script is the same as typing out the commands in the script in the current process, whereas running a script, runs it in a separate process with its own environment, so if you have a few cd
commands in a script and you run it, it doesn't change your pwd
after the script is finished, but it will if you source
it.
9 Elementary OS specific notes (v5.1)
9.1 Dark theme and additional UI tweaks
Instructions copied from here: https://github.com/elementary-tweaks/elementary-tweaks. You might get an 'add-apt-repository command not found' error. In that case, run:
sudo apt-get install software-properties-common sudo apt-get update
To install UI tweaks tool, run:
sudo add-apt-repository ppa:philip.scott/elementary-tweaks sudo apt install elementary-tweaks
You can now access the Tweaks tool under system settings. Note: You'll probably need to restart for the settings to take effect. At the very least, you'll need to restart the app
After switching to dark theme, some of the apps' title bars might still have the light variant, but I managed to fix this by following these steps:
- Install dconf-editor by running
sudo apt install dconf-editor
- Download
gtk-theme-variant-switcher
from https://github.com/beachmachine/gtk-theme-variant-switcher, cd in to the folder and runmake install
- Run dconf-editor from the applications menu and navigate to org/gtk/settings/theme-variant-switcher
- Select custom value and append a new array which consists of 3 values: WMCLASS of a window, gtk theme variant, human readable description. To get the WMCLASS of a window, run
xprop WM_CLASS
in the terminal and click on the window you want to modify. For my emacs application, I added the following array:('emacs25', 'dark', 'Emacs')
.
Some people on the internet have mentioned modifying the app's .desktop file found in /usr/share/applications/
or in ~/.local/share/applications/
by appending env GTK_THEME=elementary:dark
to the Exec
value, but that didn't work for me.
9.2 Misc bugs or inconveniences
- Right click issue on Firefox wherein the context menu gets activated on button press instead of button release. Go to
about:config
in Firefox and setui.context_menus.after_mouseup
to true - When updating applications or packages from app center, it might not get past the "Waiting for package manager lock". Running
sudo dpkg --configure -a
should fix it. - To enable Dropbox icon in the top panel,
sudo vim /etc/xdg/autostart/indicator-application.desktop
, Add;Pantheon;
to the end ofOnlyShowIn
, downloadwingpanel-indicator-ayatan
from http://ppa.launchpad.net/elementary-os/stable/ubuntu/pool/main/w/wingpanel-indicator-ayatana/ and run install the .deb package usingsudo dpkg -i Downloads/wingpanel-indicator-ayatana_2.0.3+r27+pkg17~ubuntu0.4.1.1_amd64.deb
(change package name as necessary). Log out and log in, and you should see the Dropbox icon in the top panel. You might also see a duplicated Wifi indicator, no idea how to fix that yet. - Touchpad on Acer stops working after resuming from suspend. Run the following command:
sudo /sbin/rmmod i2c_hid && sudo /sbin/modprobe i2c_hid
If that works, make it a more permanent solution:
touch /lib/systemd/system-sleep/touchpad
Add following contents to touchpad file
#!/bin/sh case $1 in post) /sbin/rmmod i2c_hid && /sbin/modprobe i2c_hid ;; esac
Make file executable chmod +x /lib/systemd/system-sleep/touchpad
and test (suspend computer and wake-up again)
10 Tools
10.1 FZF
Basic usage which fuzzy finds files in the current directory.
$ fzf
<C-r>
interactive history search, but now with fuzzy pattern matching
cd /home/projects/**<TAB>
fuzzy search files under /home/projects/
kill -9<TAB>
fuzzy search pid to kill
You can select multiple entries using fzf -m
and <TAB>
To use fd
command with fzf, add the following to .bashrc
:
export FZF_DEFAULT_COMMAND='fd --type f' export FZF_CTRL_T_COMMAND="$FZF_DEFAULT_COMMAND"
10.2 Ripgrep
Crazy fast search tool written in Rust, and runs searches in parallel by default. Ignores hidden files and files/folders mentioned under .gitignore file if one is present.
Basic usage:
rg <pattern>
Search for specific file type:
rg -thtml <pattern> rg -Thtml <pattern> #exclude html type from search