Linux Tricks
Backup¶
rsync¶
-
With rsync how can I always replace the DEST file by the SRC file?
- Use
-aflag ofrsync. - It is a quick way of saying you want recursion and want to preserve almost everything.
- If you want to always replace all the files with the same
pathonpath/to/dest/by the files onpath/to/src/, run the command:
- Use
-
With rsync how can I always replace the DEST file by the SRC file only if the SRC file is newer than the
DESTfile?- Use
-auflags ofrsync. - Skip files that are newer on the receiver (
-uflag). - If you want to replace all the files with the same
pathonpath/to/dest/by the files onpath/to/src/that are newer, run the command:
- Use
-
With rsync how can I keep the newer file between DEST and SRC file on the receiver and keep a copy on the receiver of the older of
DESTfile andSRCfile?- Use
-aubflags ofrsync. -uflag: skip files that are newer on the receiver.-bflag: with this option, preexisting destination files are renamed as each file is transferred or deleted.- If you want to keep the newer files between
path/to/dest/andpath/to/src/on the receiver (path/to/dest/) and keep a copy on the receiver (path/to/dest/) of the older files (by appending them a~), run the command:
- Use
-
With rsync how can I synchronize DEST directory and SRC directory?
- Use
-auflags ofrsync. -aflag: it is a quick way of saying you want recursion and want to preserve almost everything.-uflag: skip files that are newer on the receiver.- If you want to synchronize
path/to/src/andpath/to/dest/, that is (DESTandSRCbecome the same copy, keeping all files that were onDESTbut not onSRCand vice-versa, and keeping the newer files (with the samepath) of theDESTandSRCdirectory), run the commands:
- Use
-
With rsync how can I synchronize DEST directory and SRC directory, keep a copy of the older files and generate a log file?
- Use
-auband--backup-dirflags ofrsync. - It is a quick way of saying you want recursion and want to preserve almost everything (
-aflag). -uflag: skip files that are newer on the receiver.-bflag: with this option, preexisting destination files are renamed as each file is transferred or deleted.--backup-dir: in combination with the--backupoption, this tells rsync to store all backups in the specified directory on the receiving side.- If you want to synchronize
path/to/src/andpath/to/dest/, and keep a copy of the older files in a specified directorybackup(on both directorisepath/to/src/andpath/to/dest/) and a log filebackup.log(available on both directorisepath/to/src/andpath/to/dest/) that keep track of the synchronization process , run the commands:
rsync -aub --backup-dir="backup" path/to/dest/ path/to/src/ \ | tee -a path/to/src/backup.log rsync -aub --backup-dir="backup" path/to/src/ path/to/dest/ \ | tee -a path/to/src/backup.log rsync -aub --backup-dir="backup" path/to/dest/ path/to/src/ \ | tee -a path/to/src/backup.log rsync path/to/src/backup.log path/to/dest/backup.log- Note 1: In the file
backup.log, you can see what happen during the synchronization. This way you can see the files that have been deleted on theSRCdirectory but still present on theDESTdirectory that you copied back into to theSRCdirectory. - Note 2: The directory
backupcontains all older files. - Note 3: If on the
SRCandDESTyou had the filepath-to/some-filethat differed, after the synchronization you can see the difference (how the newer was changed from the older), by running the command:
- Note 4: After the synchronization, if you are happy, you can delete on both side (
SRCandDESTdirectories), the filebackup.logand the directorybackup. To do so, run the commands:
- Use
-
With rsync how can I delete files on DEST that are not present on SRC?
- Use
-a,--deleteflags ofrsync. - (
-aflag): it is a quick way of saying you want recursion and want to preserve almost everything. --deleteflag: this tells rsync to delete extraneous files from the receiving side (ones that aren’t on the sending side), but only for the directories that are being synchronized. You must have asked rsync to send the whole directory (e.g. "dir" or "dir/") without using a wildcard for the directory’s contents (e.g. "dir/*") since the wildcard is expanded by the shell and rsync thus gets a request to transfer individual files, not the files’ parent directory.- If you want to delete files on
path/to/dest/that are not present onpath/to/src/, run the command:
- Use
-
With rsync how can I delete files on DEST that I've note exclude the first time with rsync ?
- Use
-a,--delete-excluded,--excludeflags ofrsync. -aflag: it is a quick way of saying you want recursion and want to preserve almost everything.--delete-excludedflag: in addition to deleting the files on the receiving side that are not on the sending side, this tells rsync to also delete any files on the receiving side that are excluded with--exclude.--excludeflag: exclude files matching a pattern.- If you first have send the directories
node_modulesaccidentally from thepath/to/src/directory to thepath/to/dest/directory, you can delete them later. To do so, run the command:
- Use
-
With rsync how can I do a incremental backup?
- Use
-avz,--excludeand--exclude-excludeflags ofrsynccommand. -aflag: it is a quick way of saying you want recursion and want to preserve almost everything.-vflag: increases the amount of information you are given during the transfer.-zflag: compresses the file data as it is sent to the destination machine.--excludeflag: exclude files matching a pattern.--delete-excludedflag: in addition to deleting the files on the receiving side that are not on the sending side, this tells rsync to also delete any files on the receiving side that are excluded with--exclude.- If you want
DESTdirectory to be the exact copy ofSRCdirectory, but you just want to send the file that have changedSRC, delete onDESTthe file you have deleted onSRC, and do not send (exclude) onDESTthenode_modulesdirectory onSRC, run the command:
- Use
Backup and archive¶
-
What is a Backup?
- A backup is a copy of data that can be used to restore the original in the event that your data is lost or damaged. If a company experiences data loss due to hardware failure, human error or natural disaster, a backup can be used to quickly restore that data.
-
What is an Archive?
- An archive is a collection of historical records that are kept for long-term retention and used for future reference. Typically, archives contain data that is not actively used.
-
What is the difference between backup and archive?
- Basically, a backup is a copy of a set of data, while an archive holds original data that has been removed from its original location.
Back up /etc directory¶
-
How can I review or revert changes that were made to /etc?
- Use
etckeepertool. etckeeper: store/etcingit.- See etckeeper README.
- If you want to install and initialize
etckeeperon ubuntu, run the commands:
- If you want to
commitchanges made in/etcinstalling new package usingapt, do nothing,etckeeperdo it for you. - If you want to
commitchanges made in/etc, run the commands:
- If you want to configure and tweak
etckeeper, see files and directories/etc/etckeeper/and/etc/etckeeper/etckeeper.conf. - If you want to skip some files or directories from being stored into
gitrepository, add them to/etc/.gitignore. - If I want to revert changes to a previous state (commit), you must
checkoutto the previous commit and re-initializeetckeeper, to do so run the commands:
- Use
-
How can I back up the /etc directory to usb drive?
- Use
rsynccommand. rsync: a fast, versatile, remote (and local) file-copying tool.- Be aware that
/etcdirectory contains secret informations. For instance, SHA-512 password are stored in the file/etc/shadow.
- Use
Large files with git¶
-
What is git-annex?
-
What is git-lfs?
-
What is the difference between git-lfs and git-annex?
- See discussions git smudge clean interface suboptiomal and large files with git (article).
git-lfsdiffers fromgit-annexin that:- With
git-lfs, all the large files in the repository are usually present in the working tree; it doesn't have a way to drop content that is not wanted locally while keeping other content locally available, asgit-annexdoes. - And so
git-lfsdoes not need to be able to get content likegit-annexcan do either. git-lfsalso differs in that it uses a central server, which is trusted to retain content, so it doesn't try to avoid losing the local copy, which could be the only copy, asgit-annexdoes.
Devices¶
USB drive¶
-
How can I securely clear (wipe out) all the data on my USB drive?
- Use
ddcommand. dd: convert and copy a file.- See format usb drives by linuxize.
- If you want to wipe out all the data on your USB drive, plug it, be sure that is not mounted, and assuming that your target drive is
/dev/sdb, run the command:
- Last time, for a 32GB usb drive, the process took about 30 minutes to complete.
- Once the disk is erased, the
ddcommand will print~No space left on device.
- Use
-
How can I format a usb drive with EXT4?
- Use
partedandmkfs.ext4commands. parted: a partition manipulation program.mkfs.ext4: create an ext4 filesystem.- See format usb drives by linuxize.
- If you want to format an usb drive with only one ext4 partition that take the whole space, plug you're usb drive, be sure that is not mounted, create a GPT partition table, create a ext4 partition that takes the whole space and finaly format the partition to ext4. To do so, assuming that your target drive is
/dev/sdb, run the commands:
sudo parted /dev/sdb --script -- mklabel gpt sudo parted /dev/sdb --script -- mkpart primary ext4 0% 100% sudo mkfs.ext4 -F /dev/sdb1- When the usb drive is formated as above, when the usb is mounted it is own by
root, belong torootgroup and as the permission file isdrwxr-xr-x. - If you want to print the partition table and verify that everything is set up correctly, run the command:
- Use
-
How can I name a USB drive with an ext4 filesystem?
- Use
e2labelcommand. e2label: change the label on an ext2/ext3/ext4 filesystem.- See rename usb drive from help.ubuntu.com.
- If you want to name (or rename)
my-nameyour USB drive, assuming the target drive is/dev/sdb1and is unmounted, run the command:
- I you want to check the name of your USB drive, assuming the target is
/dev/sdb1, run the command:
- The name of an usb drive doesn't identify the usb drive. For this purpose see
UUIDof the usb drive or thePARTUUID.
- Use
-
How can I know the UUID of an USB drive?
- Use
blkidcommand. blkid: locate/print block device attributes.- See persistent block device naming from archlinux wiki.
- If you want to print the
UUIDof the block device wich partition is/dev/sdb1, run the command:
- Filesystem partitions have UUIDs but physical devices do not.
- Use
-
How can I set up the USB drive in such way that user with the same UID accross linux system can access to the USB drive?
- Use
mkfs.ext4andchowncommands. mkfs.ext4: create an ext4 filesystem.chown: change file owner and group.- See ext4 formatted usb drive on stackexchange.
- If you want to format your usb drive to ext4 file system and give it the ownership of the same username (same UID) you share accross linux system, assuming that
/dev/sdb1is the partition andmy-nameis your username, run the commands:
- Take care, the above operations will kill your all the data on the usb drive.
- Use
-
How can I preserve mode/ownership/timestamps when using cp or rsync with sudo?
cp: copy files and directories.rsync: a fast, versatile, remote (and local) file-copying tool.- If you want to copy the directory
my-dirto your USB drive mounted at/mntand preserving mode/ownership/timestamps, run the command:
-
How can I mount a usb drive without sudo privilege?
- Use
pmountcommand. pmount: mount arbitrary hotpluggable devices as normal user.- If you want to mount your USB drive on
/media/usbdriveassuming the target drive is/dev/sdb1, run the command:
- If you want to unmount your USB drive mounted with
pmountassuming the target drive is/dev/sdb1, run the command:
- Use
-
How can I mount a usb drive?
- Use
mountcommand. mount: mount a filesystem.- If you want to mount your USB drive on
/mntassuming the target drive is/dev/sdb1, run the command:
- If you want to unmount your USB drive, assuming the target drive is
/dev/sdb1, run the command:
- Use
-
How can I list block USB devices?
- Use
lsblkcommand. lsblk: list block devices.- If you want to the block USB devices on your machine, run the command:
- Use
-
How can I list USB devices?
- Use
lsusbcommand. lsusb: list USB devices.- If you want to the USB devices on your machine, run the command:
- Use
-
How can I know what is the available space on a USB drive?
- Use
dfcommand. df: report file system disk space usage.- If you want to know the available space that remains on a USB drive, assuming the target drive is
/dev/sdb1and is mounted, run the command:
- Use
-
Why the file system of my usb drive suddenly becomes read-only?
- See usb drive write protected or read only on stackexchange.
- This is probably because the drive has probably experienced a fault condition. That is your usb drive is not safe. So you must backup your data and change your usb drive.
Iphone¶
-
How can I mount my Iphone on my linux machine?
- Use
idevicepairandifusecommand. idevicepair: manage host pairings with devices and usbmuxd.ifuse: mount directories of an iOS device locally using fuse.- See mount your iphone as an external drive from maketecheasier.
- If you want to mount your Iphone on the directory
/media/iphonein order browse its directories contents, you have to plug your Iphone to your machine, pair it, create the directory/media/iphone(if doesn't exist yet) and mount the Iphone. To do so, run the commands:
- If you want to list the medias on your mobile (in my case there were on the directory
/media/iphone/DCIM/106APPLE), run the command:
- If you want to unmount your Iphone, run the command:
- If you want to install
idevicepairandifuse, run the command:
- Use
Files¶
Get info¶
-
How can I recursively list a directory in a tree-like format?
- Use
treecommand. tree: list contents of directories in a tree-like format.- In the root directory of an
Reactapp, limiting the max display depth of the directory tree to one by running the commandtree -L 1 ., I get this output:
. ├── jsconfig.json ├── node_modules ├── package.json ├── package-lock.json ├── public ├── README.md └── src 3 directories, 4 files- In the root directory of an
Reactapp, limiting the max display depth of the directory tree to one and ignoring file in thenode_modulesdirectory, by running the commandtree -I node_modules -L 2 ., I get this output:
. ├── jsconfig.json ├── package.json ├── package-lock.json ├── public │ ├── discreet-theme.css │ ├── icon-tricks192.png │ ├── icon-tricks512.png │ ├── index.html │ ├── manifest.json │ └── robots.txt ├── README.md └── src ├── App.js ├── App.scss ├── App.test.js ├── astUtils.js ├── astUtils.test.js ├── index.js ├── _myBulma.scss ├── orgContent.js ├── serviceWorker.js └── setupTests.js 2 directories, 20 files - Use
-
How can I check the space usage of a directory?
- Use
ducommand. du: estimate file space usage.- If you want to check the space usage of your
$HOMEdirectory in a human readable way, run the command:
- Use
-
How can I check the file type of a file?
- Use
filecommand. file: determine file type.- If you want to check the file type of the files
elisp.elandvideo.mp4, that respectively containemacs-lispcode andmp4video, run the command:
- After running the previous command, I got this output:
- Use
-
How can I get a detailed information about audio/video files?
- Use
mediainfocommand. mediainfo: command line utility to display information about audio/video files.- After running the command
mediainfo video.mp4, I got the output:
General Complete name : video.mp4 Format : MPEG-4 Format profile : Base Media Codec ID : isom (isom/iso2/mp41) File size : 186 KiB Duration : 11 s 603 ms Overall bit rate mode : Constant Overall bit rate : 131 kb/s Writing application : Lavf57.83.100 Audio ID : 1 Format : AAC Format/Info : Advanced Audio Codec Format profile : LC Codec ID : mp4a-40-2 Duration : 11 s 603 ms Duration_LastFrame : -3 ms Bit rate mode : Constant Bit rate : 129 kb/s Channel(s) : 2 channels Channel positions : Front: L R Sampling rate : 48.0 kHz Frame rate : 46.875 FPS (1024 SPF) Compression mode : Lossy Stream size : 183 KiB (98%) Default : Yes Alternate group : 1- After running the command
mediainfo audio.mp3, I got the output:
General Complete name : audio.mp3 Format : MPEG Audio File size : 2.31 MiB Duration : 2 min 45 s Overall bit rate mode : Variable Overall bit rate : 117 kb/s Writing library : Lavf57.83.100 Audio Format : MPEG Audio Format version : Version 1 Format profile : Layer 3 Format settings : Joint stereo Duration : 2 min 45 s Bit rate mode : Variable Bit rate : 117 kb/s Channel(s) : 2 channels Sampling rate : 48.0 kHz Frame rate : 41.667 FPS (1152 SPF) Compression mode : Lossy Stream size : 2.31 MiB (100%) - Use
-
What option can I use to format the file time printed with -l option of ls command?
- Use
--time-styleoption oflscommand: --time-style: with-l,lsshow time using stylefull-iso,long-iso,iso,locale, or+FORMATwhere FORMAT is interpreted like indate.- If you want to list the file of a directory with the time format set to
iso, run the command:
- After running the previous command on one of my directory, I got this output:
- Use
View files¶
-
What cat, head and tail commands are for?
cat: concatenate files and print on the standard output.head: output the first part of files.tail: output the last part of files.
-
What less command is for?
less: a filter for paging through text one screenful at a time.
Links¶
-
How can make a symbolic link?
- Use
lncommand. ln: make links between files.- If you want to make a link to the file
/home/tony/videos/video.mp4(TARGET) with the namevideo.mp4(LINK_NAME) living in the directorylinked-video, run the commands:
- If the
linked-videowas empty before making the link above and if you run the commandls -lin the directorylinked-videoyou should obtain something like this:
- Use
-
How can I get the path (target) of a symbolic link?
- Use
readlinkcommand. readlink: print resolved symbolic links or canonical file names.- If you want to get the path (target) of the current
python, run the command:
- Use
-
How can I manage symbolic links of whole directory tree?
- Use
stowcommand. stow: software package installation manager.-
If you want to link all your dotfiles files in your
$HOMEdirectory to a versioned directorydotfiles, assumingdotfilesdirectory is a subdirectory of~/some-directory, run the commands: -
If you want to unlink the
stowlinks made in the previous section, run the commands:
- Use
Compression¶
-
How can I (archive) store multiple files in a single file?
- Use
tarcommand. tar: an archive utility.- If you want to store all files frome the directory
/etcinto the archive fileetc.tarverbosely listing the files being archived, run the command:
- Use
-
How can I extract all files from a tar archive?
- Use
tarcommand. tar: an archive utility.- If you want extract all files from the archive
archive.tar, run the command:
- Use
-
What is the difference between zip and gzip?
- See difference between zip and gzip from differencebetween.net.
gzipcan achieve better compression compared tozip.zipis capable of archiving and compressing multiple files, whilegzipis only capable of compression.- You can easily extract individual files from a large
zipfile, but not from agziptarball.
Search¶
-
What is fzf?
- fzf is a general-purpose command-line fuzzy finder.
- See fzf repository and this video on fzf.
-
How to update linux locate cache?
- Use
updatedbcommand. updatedb: Update a database formlocate.- See stackexchange.
- If you want to update the database use by
locatecommand, run the command:
- Use
-
What locate command is for?
locate: find files by name.
-
What grep command is for?
grep: Search for pattern in files.- If you want to search the sequence
hello worldin the filesmenu.handmain.cignoring the case, run the command:
-
How to ignore binary files when searching with grep?
- Use
-Ioption ofgrepcommand. -I: Process a binary file as if it did not contain matching data.- See stackoverflow.
- If you want to search the sequence
hello worldin the files in your current directory ignoring the binary files, run the command:
- Use
-
How to find files that match string or regexp?
- Use
findcommand. find: search for files in a directory hierarchy.- See example of command find by Alvin Alexander.
- If you want to find files recursively from your
$HOMEdirectory where thenamematch the global paternvirtualenv*, run the command:
- Use
-
What is recoll?
recollis a desktop full-text search tool.- See recoll homepage, recoll usermanual and counsel-recoll if you want use it with emacs.
- To install
recoll, run the command:
~/.recoll/is the default config directory.~/.recoll/recoll.confis therecollmain configuration file.~/.recoll/xapiandb/is the default directory for the index data.- To use it, you first have to index the
xiapandatabase withrecollindex. - To index the
xiapandatabase ofrecollaccording to the the config file~/.recoll/recoll.conf, run the command:
Replace string¶
-
How to replace string in multiple files?
- Use commands
sed,perlandfind. sed: stream editor for filtering and transforming text.perl: the Perl 5 language interpreter.find: search for files in a directory hierarchy.- See replace string on stackoverflow.
- If you want to replace all occurrences of one string with another in all files in the current directory (Non recursive, files in this directory only.), run the command:
- If you want to replace all occurrences of one string with another in all files in the current directory (Recursive, regular files (including hidden ones) in this and all subdirectories), run the command:
- If you want to replace only if the file name matches another string (Non-recursive, files in this directory only), run the command:
- If you want to replace only if the file name matches another string / has a specific extension / is of a certain type etc (Non-recursive, files in this directory only), run the command:
- Use commands
fd command¶
-
With fd, how to find all files except hidden files and directories?
- It's the default behaviour of
fdcommand. fd: find entries in the filesystem.- See fd repository.
- If you want to find all files except hidden files and directories, run the command:
- Note that if you are in
gitdirectory, the search ignores the files ignored by.gitignorefile.
- It's the default behaviour of
-
With fd, how to include all hidden files in the search?
- Use the option
--hiddenoffdcommand. fd: find entries in the filesystem.- See fd repository.
- If you want to include all hidden files in your
fdsearch, run the command:
-
Note that if you are in a
gitdirectory, this shows all files in the directory.git/but skip the files ignored by.gitignorefile. -
If you want to include all hidden files in your
fdsearch but exclude files in the.git/directory, run the command:
- Use the option
-
With fd, how to find every file entries?
- Use
--hiddenand--no-ignoreoption offdcommand. fd: find entries in the filesystem.- See fd repository.
- If you want to include every file entries in your
fdsearch, run the command:
- Use
-
With fd, how to restrict the search to the directories?
- Use
--typeoption offdcommand. fd: find entries in the filesystem.- See fd repository.
- If you want to restrict your
fdsearch to the directories, run the command:
- Use
-
With fd, how to restrict the search to the file only?
- Use
--typeoption offdcommand. fd: find entries in the filesystem.- See fd repository.
- If you want to restrict your
fdsearch to the files only, run the command:
- Use
Directories¶
-
How can I list the directories first when using ls?
- Use
--group-directories-firstflag oflscommand. ls: list directory contents.- If you want to list the directories first when using
ls, run the command:
- Use
-
How can I create a directory with its parents?
- Use
mkdircommand. mkdir: make directories.- If you want to create this nested directories
dir-1/dir-2/dir-3from the directory you are in, run the command:
- Use
-
How can I go back to the last directory I was in?
- Use
cdcommand. cd: change the shell working directory.- I you want to go back to the last directory you were in, run the command:
- Use
Watch files¶
-
How can I automatically run test when some files change on disk?
- Use
watchman-makecommand that come withwatchman. watchman-make: waits for changes to files and then invokes a build tool (by default,make) or provided script to process those changes.- See watchman documentation and watchman install documentation.
- If you want to run
pyteston a python project each time the subdirectories./srcand./tests/change on your disk, in the root of your project, run the command:
- To install
watchman-makewhich is part of the packagewatchman, use the package managerhomebrewand run the command:
- Use
-
How can I whatch a file and trigger a command when it was opened?
- Use
inotifywaitcommand. inotifywait: wait for changes to files using inotify.- If you want to print
hello worldin thestdoutafter the filepath/to/watched-filewas opened, run the command:
- Use
Redirection¶
-
What does 2>&1 mean in a command line?
- Redirect the standard error
stderr(the file descriptor 2) to the standard outputstdout(the file descriptor 1). You can consider>&as a redirect merger operator. - See stackoverflow.
- So if you have a command line
my-commandthat send error tostderrand you want to catch the errors onstdout, run this command:
- Redirect the standard error
Images¶
Use images¶
-
How to set the wallpaper?
- Use the command
feh. feh: Image viewer and cataloguer.- See feh on archlinux wiki.
- If you want
path/to/images.jpgto be you be your image background, run the command:
- If you want to remove your background image set with
feh, run the command:
- Use the command
Manipulate images¶
-
How to convert images?
- Use the command
convert. convert: convert between image formats as well as resize an image.- See convert image on howtogeek.
- If you want to convert the image
image.pngto the formatjpg, run the command:
- If you want to install
convert, as it comes with the imagemagick package, run the command:
- Use the command
-
How to resize an image?
- Use the command
convert. convert: convert between image formats as well as resize an image.- See convert image on howtogeek.
- If you want to resize an image to 200 pixels in width and 100 pixels in heigh, run the command:
- If you want to install
convert, as it comes with the imagemagick package, run the command:
- Use the command
Miscellaneous¶
Links¶
-
Where can I find linux ressources?
- From linuxize the following articles are worth reading: cron jobs, setup git server, setup ftp server, setup sftp, add apt repository, xargs command, watch command, /etc/shadow file, change hostname on ubuntu, size of file and directory.
- computer hostname: from whatismyipaddress.com.
- Why use sticky bit?: on askubuntu.
- passwd command line and POSIX: wikipedia articles.
- docopt: a cli description language. It helps you define the interface for your command-line app and automatically generate a parser for it.
- linux commands: great list of 93 various command lines by linoxide.
- Desktop_environment and Window_manager: archlinux wiki pages.
- real-time signals: All about linux signals by Linux Programming Blog.
terminal¶
-
How can I check which terminal I'm using?
- Use
catandgrepcommands. cat: concatenate files and print on the standard output.grep: print lines matching a pattern.- See which terminal i am using on askubuntu.
- If you want to check which terminal you are using, run the command:
- Use
desktop¶
-
How can I send a desktop notification?
- Use
notify-sendcommand. notify-send: a program to send desktop notifications.- If you want to send the message
hello worldto the desktop and that the popup window notification keep alive 3 seconds, run the command:
- Use
environment variables¶
-
How can I print all environment variables?
- Use
printenvcommand. printenv: print all or part of environment.- If you want to print all environment variables, run the command:
- Use
-
How can I list colors used in my terminal?
- Use
dircolorscommand. dircolors: color setup forls.- If you want to print the color used by
lscommand in your terminal, run the command:
- Use
Ubuntu¶
-
How to check your ubuntu version?
- Use
lsb_releasecommand. lsb_release: Print distribution-specific information.- See linuxize.
- If you want to know your Ubuntu distribution version, run the command:
- Use
Network¶
DNS¶
-
How to get information of an url ?
- Use the command
dig. dig: a DNS lookup utility.- See A record from dnsimple.com.
- If you want to get the A record of
youtube.com, run the command:
- Use the command
Host¶
-
How can I know my IP address?
- Use
ifconfigcommand. ifconfig: configure a network interface.- If you want to know your (local) IP address (use over the LAN), run the command:
- If you want to know your (public) IP address (use over internet), follow the link ifconfig.co or run the command:
- Use
SSH¶
-
How can I enable SSH server on ubuntu?
- Use
openssh-serverpackage. openssh-server: Secure shell (SSH) server, for secure access from remote machines.- See enable ssh on ubuntu by linuxize.
- If you want to enable SSH server on the standard port 22, you have to start
sshservice withsystemctlcommand and allow incomming SSH connection by opening (allowing) the port 22 with the commandufwand enabling the connection with the commandufw. To do so, run the commands:
- If you want to install
openssh-serverand start the servicessh, run the command:
- Use
-
How can I connect to my server via SSH?
- Use
sshcommand. ssh: OpenSSH SSH client (remote login program).- See enable ssh on ubuntu by linuxize.
- If you want to connect to your Ubuntu machine over LAN with
bobas username and192.168.1.100IP, run the command:
- If you want to connect to your Ubuntu machine over Internet with
bobas username and2a01:c50e:e911:9600:fc4d:b07d:d36f:6358IP, run the command:
- Use
-
How can I know that my shell is connected via a SSH connection?
- Use
printenvcommand. printenv: print all or part of environment.- If you want to know if you're connected via SSH, check if the environment variables
SSH_CONNECTION,SSH_CLIENTandSSH_TTYare defined. To do so, run the command:
- Use
-
How can I exit a SSH connection?
- Use
exitcommand. exit: cause normal process termination.- If you want to exit a SSH connection, you can either type
C-dor run the command:
- Use
-
How can I mount remote directories over ssh?
- Use
sshfscommand. sshfs: filesystem client based on ssh.- See sshfs by linuxize.
- If you want to mount the home directory of a user named
tonyon a remote host with IP address of192.168.1.100, create a directory~/remote-tonythat will serve as a mount point and usesshfscommand. To do so, run the commands:
- If you want to detach the mounted file system
~/remote-tony, run the command:
- Use
-
How can I setup a passwordless SSH login?
- Use
ssh-keygen,ssh-addandssh-copy-idcommand. ssh-keygen: Authentication key generation, management and conversion.ssh-add: Adds private key identities to the authentication agent.ssh-copy-id: Use locally available keys to authorise logins on a remote machine.- See examples setup passwordless ssh login and create different public key.
- If you want to connect to the user
jimon the server192.168.1.100without prompting a password, you have to generate a ssh key, send the public part tojim@192.168.1.100server, append the public key on the remote file~/.ssh/authorized_keysand add the private key to the authentication agent. After that, you can directly connect to the server. To do so, run the command:
ssh-keygen -t rsa -f ~/.ssh/id_rsa_jim ssh-copy-id -i ~/.ssh/id_rsa_jim.pub jim@192.168.1.100 ssh-add ~/.ssh/id_rsa_jim- If you want to list all identities currently represented by the agent, run the command:
- If you want to delete the identity
~/.ssh/id_rsa_jimfrom the agent, run the command:
- Use
-
How can I solve the problem that give the error: "Error connecting to agent: No such file or directory"?
- Use
evalcommand. eval: execute arguments as a shell command.- See open a connection to your authentication agent on stackexchange.
- If you want to open a connection to your authentication agent that is already running, run the command:
- Use
-
How can I start my authentication agent ssh-agent?
- Use
ssh-agent,evalandssh-addcommands. ssh-agent: hold private keys used for public key authentication.eval: execute arguments as a shell command.ssh-add: adds private key identities to the authentication agent.- If you want to start your authentication agent, open a connection to the agent and add the key
id_rsa_some_keyto the agent. To do so, run the commands:
- If you want to list the identities represented by the authentication agent, run the command:
- If you want to Kill the current agent (given by the
SSH_AGENT_PIDenvironment variable), run the command:
- Use
-
How to get ssh private key automatically added to the authentication agent?
- Use the keywords
AddKeysToAgentandIdentityFilein the~/.ssh/configuser's configuration file. AddKeysToAgent: specifies whether keys should be automatically added to a running ssh-agent(1).IdentityFile: specifies a file from which the user's RSA authentication identity is read.- See
mandocumentationssh_config. - If you want to get ssh private keys
~/.ssh/id_rsa_key_1and~/.ssh/id_rsa_key_2automatically added to the authentication agent, you have to add these following lines to the file~/.ssh/config:
- Use the keywords
-
How can I start ssh-agent on login?
- Use
systemdby adding a user systemd unit forssh-agent. - See start-ssh-agent-on-login on stackoverflow.
-
If you want to start
ssh-agenton login, you have to add assh-agent.serviceuser systemd unit, exportSSH_AUTH_SOCKenvironment in the.bashrcfile,enableandstartthessh-agentunit, and specify (in the file~/.ssh/config) that keys should be automatically added to a runningssh-agent. -
file
~/.config/systemd/user/ssh-agent.service:
[Unit] Description=SSH key agent [Service] Type=forking Environment=SSH_AUTH_SOCK=%t/ssh-agent.socket ExecStart=/usr/bin/ssh-agent -a $SSH_AUTH_SOCK [Install] WantedBy=default.target- file
.bashrc:
- in a terminal, run the commands:
- file
~./ssh/config, if you have two private keys:
- Note that after login, before the first use of
ssh(viassh,git, …), yourssh-agentdoesn't know about your keys, sossh-add -llists anything.
- Use
Servers¶
-
How to run a php server?
- Use
phpcommand. php: PHP Command Line Interface.- If the directory contains a
index.phpfile, to have the server running onlocalhoston port8080, run the command:
- Use
-
How to run a server that automatically reload when files change on the system?
- Use the command
browser-sync. - See browsersync homepage.
- If the directory contains an
index.htmlfile, to have the server reload each second, run the command:
- If the browser tab doesn't refresh automatically when you modify files, then you have to add a snippet into your website, just before the closing
</body>tag. If you runbrowser-sync startcommand line, this prints the snippet you have to add to yourindex.htmlfile. When I did, this gave me that snippet:
<script id="__bs_script__">//<![CDATA[ document.write("<script async src='http://HOST:3000/browser-sync/browser-sync-client.js?v=2.26.13'><\/script>".replace("HOST", location.hostname)); //]]></script>- If you want to install
browser-sync, run the command:
- Use the command
-
How can I rewrite apache rules?
- Use
RewriteCondandRewriteRuledirectives. - See module mod_rewrite from Apache documentation.
- Use
-
How can I configure OVH config file on server?
- Modify the
.ovhconfigfile. - See .ovhconfig file from OVH documentation.
- Modify the
-
Where can I find good nginx resources?
- install nginx, nginx reverse proxy, nginx server block, nginx ssl by linuxize.
- reverse proxy, nginx config pitfalls, nginx serving statict content, ssl nginx / upstream server from nginx.com.
- nginx rewrite rules from nginx.com that shows really good examples to understand what are
$uri,$request_uri,try_files,return,rewrite. - improve your nginx ssl from scaron.info.
-
What does request URI refer to in nginx vocabulary?
- The request URI is the part of the request that comes after the domain name or IP address/port.
-
How can I install nginx on ubuntu?
- Use
aptandufwcommand. apt: high-level commandline interface for the package management system.ufw: program for managing a netfilter firewall.- See install nginx by linuxize.
- If you want to install nginx on ubuntu and get ready to use, update your packages with
apt update, install it withapt installand open theportsused by nginx:
- If you want to check if
nginxis running, run the command:
- Each time you modify your
nginxconfiguration, you have toreloadnginx. To do so, run the command:
- Use
-
What is the default configuration of nginx after installing it?
- See install nginx by linuxize.
- By default,
nginxserves the fileindex.nginx-debian.htmlonlocalhost(127.0.0.1) onport 80. Just after intallingnginx, before any modification, you can visit the addresshttp://127.0.0.1on your browser. - This happens because
nginxlooks at config files on the directory/etc/nginx/sites-enabled. It sees the file/etc/nginx/sites-enabled/defaultthat is a symlink of the file/etc/nginx/sites-available/default. This default file tellsnginxto serve the fileindex.nginx-debian.htmlthat is in the directory/var/www/html. And the config file default, after flushing the commented lines, is as follow:
-
How can I set nginx to be a reverse proxy?
- Use
proxy_passdirective in yournginx blockconfiguration. - To pass a request to an HTTP proxied server, the
proxy_passdirective is specified inside alocation. - See nginx reverse proxy by linuxize, reverse proxy from nginx.com and proxy_pass from nginx.org documentation.
- If you want to serve at
mydomain.coman application that is running as a http server (node app, flask app,…) onhttp://127.0.0.1:5000, yournginx blockformydomain.comdefined in the file/etc/nginx/sites-available/mydomain.confmust contain the following lines:
- Use
-
How can I fix the nginx error "error (nginx): cannot get /app" related to proxy servers?
- See
proxy_passdirective in yourblockconfiguration. - The error was that I was trying to pass
location /appto the addresshttp://127.0.0.1:5000without putting a/a the end of the address.
-To solve the problem, I add a
/to theproxy_passaddress. Myblockconfiguration/etc/nginx/sites-available/mydomain.confcontains know the following lines: - See
Connection¶
-
How can I restart my wifi connection?
- Use
servicecommand. service: run a System V init script.- See restart wifi connection on askubuntu.
- If you want to restart your wifi connection, run the command:
- Use
-
How can I check if I'm connected to a wireless network?
- Use
iwgetidcommand. iwgetid: report ESSID, NWID or AP/Cell Address of wireless network.- If you want to check if you are connected to a wireless newtork, run the following command and if you get nothing, you are not connected:
- Use
ftp¶
-
Why do I get kicked out of a FTP session once I run a command?
- Use
ufwcommand. ufw: program for managing a netfilter firewall.- See ftp error due to firewall on stackexchange.
- If you want to open all the port to not have problem of firewall with
ftp, run the command:
- Use
-
What ftp command is for?
ftpis an internet file transfer program.
Miscellaneous¶
-
What curl command is for?
curl: transfer a URL.
-
What wget command is for?
wget: the non-interactive network downloader.
-
What ping command is for?
ping: send ICMP ECHO_REQUEST to network hosts.
Package management¶
Linux packages¶
-
How can I list linux packages related to a regexp?
- Use
apt-cache searchcommand. apt-cache search: perform a full text search on all available package lists for the POSIX regex pattern given.- If you want to search linux packages related to
emacs, run the command:
- Use
-
How can I show linux package information?
- Use
apt-cache showcommand. apt-cache show: display the package records for the named packages.- If you want to show the information of the installed
emacspackage, run the command:
- Use
-
How can I update the list of available linux packages?
- Use
apt-get updatecommand. apt-get update: this command is used to resynchronize the package index files from their sources.
- Use
-
How can I list the installed packages?
- Use
apt listcommand. apt list: list package satifying certain criteria.- See list installed packages by linuxize.
- If you want to list all installed packages, run the command:
- If you want to check if the package
emacsis installed, run the command:
- Use
-
How can I install newest versions of all my packages?
- Use
apt-get upgradecommand. - This command installs the newest versions of all packages currently installed on the system from the sources enumerated in
etc/apt/sources.listfile.
- Use
-
How can I install a new linux package?
- Use
apt-get installcommand. apt-get install: install new one or more linux packages.- If you want to install
emacs, run the command:
- Use
-
How can I remove a linux package?
- Use
apt-get removecommand. apt-get remove: remove linux packages. Note that removing a package leaves its configuration files on the system.- If you want to remove
emacs, run the command:
- Use
-
How can I get the list of the libraries a command depends on?
- Use
lddcommand. ldd: print shared object dependencies.- If you want to list the library dependencies of the
lscommand, run the command:
- On my computer the previous command gave me:
linux-vdso.so.1 (0x00007ffeeb143000) libselinux.so.1 => /lib/x86_64-linux-gnu/libselinux.so.1 (0x00007f2203fc5000) libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f2203bd4000) libpcre.so.3 => /lib/x86_64-linux-gnu/libpcre.so.3 (0x00007f2203962000) libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f220375e000) /lib64/ld-linux-x86-64.so.2 (0x00007f220440f000) libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f220353f000) - Use
npm for nodejs¶
-
How can I list the the nodejs installed packages?
- Use
lssubcommand ofnpm. npm ls: Print packages' version that are installed, as well as their dependencies, in a tree-structure.- If you want to list the installed packages in your
nodeproject, in root directory of your project run the command:
- If you want to list the
nodeinstalled packages globaly in your machine, run the command:
- If you want to list only the name@version of the installed packages in your
nodeproject, you have to limite the depth of the printed tree. To do so, run the command:
- Use
pip for python¶
-
What is pip?
pipis a package installer for python. You can usepipto install packages from the Python Package Index and other indexes.- See pip on pypi.
brew for missing packages on macOS¶
-
How can I install brew?
- See brew homepage.
- To install
brew, run the command:
nix¶
-
What is nix?
Process and system¶
pm2¶
-
How can I install pm2?
- Use
npmcommand. npm: javascript package manager.- If you want to install
pm2globaly, run the command:
- If you want to start your
app.jsapplication, run the command:
- Use
-
How can I start and daemonize applications with pm2?
- Use
pm2 startcommand. pm2 start: start and daemonize an app.- See quick start from the documentation.
- If you want to start and daemonize with
pm2your application that normally start by runningnpm start, run the command:
- If you want to start and daemonize with
pm2your application that normally start by runningnpm start -- --port=5000, run the command:
- If you want to start and daemonize with
pm2your application that normally start by runningnpm startand give it the namemy-apprun the command:
- Use
-
How can I automatically restart application on boot/reboot?
- Use
pm2 startupandpm2 savecommands. pm2 startup: enable the pm2 startup hook.pm2 save: freeze a process list for automatic respawn.- See quick start from the documentation.
- If you want that
pm2automaticallyrestarton boot/reboot and run the saved processes, you have to enable thepm2startup hook and freeze a list of the current running processes. To do so, run the commands:
- If you want to revert the action
pm2 startup, run the command:
- Use
-
What are the common actions I want to do with pm2?
- See documentation and
pm2 --help. - List of commands, without description:
pm2 example pm2 list pm2 show <name|id> pm2 stop <name|id> pm2 delete <name|id> pm2 restart <name|id> pm2 status pm2 start pm2 logs- List of options, without description:
--watch,--ignore-watch,--watch-delay.
- See documentation and
CPU¶
-
How can I know the caracteristics of my CPU?
- Use
lscpucommand. lscpu: display information about the CPU architecture.- See number of cores of as system on stackexchange.
- If you want to know how many physical CPU do you have, run the command
lscpuand see the line which starts withSocket(s):. - If you want to know how many core per socket do you have, run the command
lscpuand see the line which starts withCPU(s):. - After running the command
lscpu, I get this output:
Architecture: x86_64 CPU op-mode(s): 32-bit, 64-bit Byte Order: Little Endian CPU(s): 4 On-line CPU(s) list: 0-3 Thread(s) per core: 2 Core(s) per socket: 2 Socket(s): 1 NUMA node(s): 1 Vendor ID: GenuineIntel CPU family: 6 Model: 69 Model name: Intel(R) Core(TM) i5-4300U CPU @ 1.90GHz Stepping: 1 CPU MHz: 1046.894 CPU max MHz: 2900.0000 CPU min MHz: 800.0000 BogoMIPS: 4988.47 Virtualization: VT-x L1d cache: 32K L1i cache: 32K L2 cache: 256K L3 cache: 3072K NUMA node0 CPU(s): 0-3 Flags: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc cpuid aperfmperf pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm cpuid_fault epb invpcid_single pti ssbd ibrs ibpb stibp tpr_shadow vnmi flexpriority ept vpid ept_ad fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid xsaveopt dtherm ida arat pln pts md_clear flush_l1d - Use
hardware¶
-
How can I get the characteristics of my RAM?
- Use
lshwcommand. lshw: extract detailed information on the hardware configuration of the machine.- See frequency and type of my current RAM on askubuntu.
- If you want to get characteristics of your RAM, run the command:
- After running the previous command, I got this output:
*-firmware description: BIOS vendor: Hewlett-Packard physical id: c version: L71 Ver. 01.07 date: 02/09/2014 size: 64KiB capacity: 8128KiB capabilities: pci pcmcia upgrade shadowing cdboot bootselect edd int5printscreen int9keyboard int14serial int17printer acpi usb smartbattery biosbootspecification netboot uefi *-cache:0 description: L1 cache physical id: 2 slot: Unknown size: 32KiB capacity: 32KiB capabilities: asynchronous internal write-back instruction configuration: level=1 *-cache:1 description: L2 cache physical id: 3 slot: Unknown size: 256KiB capacity: 256KiB capabilities: asynchronous internal write-back unified configuration: level=2 *-cache:2 description: L3 cache physical id: 4 slot: Unknown size: 3MiB capacity: 3MiB capabilities: asynchronous internal write-back unified configuration: level=3 *-cache description: L1 cache physical id: 1 slot: Unknown size: 32KiB capacity: 32KiB capabilities: asynchronous internal write-back data configuration: level=1 *-memory description: System Memory physical id: 7 slot: System board or motherboard size: 4GiB *-bank:0 description: SODIMM DDR3 Synchronous 1600 MHz (0.6 ns) product: HMT351S6EFR8A-PB vendor: Hynix/Hyundai physical id: 0 serial: 00821224 slot: Bottom-Slot 1(left) size: 4GiB width: 64 bits clock: 1600MHz (0.6ns) *-bank:1 description: DIMM [empty] physical id: 1 slot: Bottom-Slot 2(right) - Use
-
How can I list the hardwares of my system?
- Use
lshwcommand. lshw: extract detailed information on the hardware configuration of the machine.- I you want to list the hardwares of your system, run the command:
- After running the previous command, I got this output:
H/W path Device Class Description ====================================================== system HP EliteBook 840 G1 (F9X37UP#ABF) /0 bus 198F /0/c memory 64KiB BIOS /0/0 processor Intel(R) Core(TM) i5-4300U CPU @ 1.90GHz /0/0/2 memory 32KiB L1 cache /0/0/3 memory 256KiB L2 cache /0/0/4 memory 3MiB L3 cache /0/1 memory 32KiB L1 cache /0/7 memory 4GiB System Memory /0/7/0 memory 4GiB SODIMM DDR3 Synchronous 1600 MHz (0.6 ns) /0/7/1 memory DIMM [empty] /0/100 bridge Haswell-ULT DRAM Controller /0/100/2 display Haswell-ULT Integrated Graphics Controller /0/100/3 multimedia Haswell-ULT HD Audio Controller /0/100/14 bus 8 Series USB xHCI HC /0/100/14/0 usb2 bus xHCI Host Controller /0/100/14/0/3 bus USB2134B /0/100/14/0/3/3 input USB OPTICAL MOUSE /0/100/14/0/3/4 input USB Keyboard /0/100/14/0/5 generic VFS495 Fingerprint Reader /0/100/14/0/7 multimedia HP HD Webcam /0/100/14/1 usb3 bus xHCI Host Controller /0/100/14/1/3 bus USB5534B /0/100/16 communication 8 Series HECI #0 /0/100/16.3 communication 8 Series HECI KT /0/100/19 enp0s25 network Ethernet Connection I218-LM /0/100/1b multimedia 8 Series HD Audio Controller /0/100/1c bridge 8 Series PCI Express Root Port 1 /0/100/1c.3 bridge 8 Series PCI Express Root Port 4 /0/100/1c.3/0 wlo1 network Wireless 7260 /0/100/1c.5 bridge 8 Series PCI Express Root Port 6 /0/100/1c.5/0 generic RTS5227 PCI Express Card Reader /0/100/1d bus 8 Series USB EHCI #1 /0/100/1d/1 usb1 bus EHCI Host Controller /0/100/1d/1/1 bus USB hub /0/100/1f bridge 8 Series LPC Controller /0/100/1f.2 storage 8 Series SATA Controller 1 [AHCI mode] /0/100/1f.3 bus 8 Series SMBus Controller /0/2 scsi0 storage /0/2/0.0.0 /dev/sda disk 180GB INTEL SSDSC2BF18 /0/2/0.0.0/1 /dev/sda1 volume 167GiB EXT4 volume /1 power CM03050XL - Use
-
What is dual channel memory?
- See What is dual channel memory from Laptop Memory Upgrade.
-
How to upgrade my pc RAM?
- See how to upgrade or replace pc RAM from How-To Geek.
systemd¶
-
How do I reload the configuration file of a systemd unit?
- Use
daemon-reloadsubcommand ofsystemctl. systemctl daemon-reload: reload the systemd manager configuration.- If you want your changes in
myservice.servicesystemd unit file to be reload, run the command:
- Use
-
What is systemd?
systemd: a system and service manager for Linux operating systems.
-
What systemctl command is for?
systemctl: control the systemd system and service manager.- See systemd from Archlinux wiki.
- Common command lines using
systemctl:
-
What journalctl command is for?
journalctl: Query the systemd journal.
ps command¶
-
What ps command is for?
ps: report a snapshot of the current processes.
-
What does STAT column refer to in the output of ps command?
- This column indicate the process states.
- See stackexchange.
- If I run the command
ps -x, the first 8 lines of the output I get are:
PID TTY STAT TIME COMMAND 1104 ? Ssl 0:00 PM2 v4.2.3: God Daemon (/home/tony/.pm2) 1647 ? Ss 0:01 /lib/systemd/systemd --user 1648 ? S 0:00 (sd-pam) 1663 ? Ss 0:00 /usr/bin/ssh-agent -a /run/user/1000/ssh-agent.socket 1670 tty2 Ssl+ 0:00 /usr/lib/gdm3/gdm-x-session --run-script i3 1672 tty2 Sl+ 1:18 /usr/lib/xorg/Xorg vt2 -displayfd 3 -auth /run/user/1 1687 ? Ss 0:00 /usr/bin/dbus-daemon --session --address=systemd: --n 1690 tty2 S+ 0:01 i3- Processes states that ps indicates are:
D Uninterruptible sleep (usually IO) R Running or runnable (on run queue) S Interruptible sleep (waiting for an event to complete) T Stopped, either by a job control signal or because it is being traced. W paging (not valid since the 2.6.xx kernel) X dead (should never be seen) Z Defunct ("zombie") process, terminated but not reaped by its parent.- and the additional characters are:
-
What does TTY mean in the command ps?
- A
TTYis a computer terminal. In the context ofps, it is the terminal that executed a particular command. - The abbreviation stands for TeleTYpewriter, which were devices that allowed users to connect to early computers.
- See stackoverflow.
- A
socket¶
-
What is a socket?
- A
Unix domain socketorIPC socketis a data communications endpoint for exchanging data between processes executing on the same host operating system. - See wikipedia and stackexchange.
- A
-
Where is my emacs server socket?
- In the directory
/var/run/user/1000/emacs/.
- In the directory
miscellaneous¶
-
What hostnamectl command is for?
hostnamectl: control the system hostname.
-
What top command is for?
top: display Linux processes.
-
How can I check the PID number of my emacs process?
- Use
pidofcommand. pidof: Find the process ID of a running program.- See process name and pid (tecmint) and pidof (linuxize).
- If you have an
emacsprocesse and you want to check itsPIDnumber, run the command:
- Use
Scripts¶
Makefile¶
-
Where can I find a good tutorial on Makefile?
- See makefile tutorial.
-
What is a Makefile?
- See What is a Makefile? by Sachin Patil.
-
How to use wildcard in Makefile?
- Use the function
wildcardof themakesyntax. - See wildcard in makefile on stackoverflow.
- If you have two source files
foo.candbar.c, to set the value of the variableprogramswithfoo.c bar.c, add this line to yourMakefile:
- Use the function
bash script¶
-
Where can I find a good cheatsheet about bash scripting?
- See bash scripting cheatsheet on devhints.io.
-
How can I know the status of the last command run in my terminal?
- Use
$?symbol andechocommand. echo: display a line of text.- See bash functions.
- If you want to print the status of the last command run in your terminal, run the command:
- When a bash function completes, its return value is the status of the last statement executed in the function,
0for success and non-zero decimal number in the1 - 255range for failure.
- Use
Format outputs¶
-
How can I format numbers in bash script?
- Use
printfcommand. printf: format and print data.- See formatting numerical output in bash.
- If you want to transform
12.3to12and3.62to03, theprintfformat to use is%02.0f. So, running the following command gives you the expected results:
- Use
UI¶
GTK¶
-
How to make chromium using GTK?
- If you want that
chromiumusesGTK, inchromiumallowGTK+theme in:Settings > Preferences > Appearence > Use GTK+ theme.
- If you want that
-
What are the GTK config files?
~/.gtkrc-2.0forgtk2,~/.config/gtk-3.0/settings.iniforgtk-3,/etc/gtk-3.0/settings.ini.
-
How to get information on the GTK settings?
- Run the command:
-
Where are stored the GTK themes?
- You can find
GTKthemes in the directory:/usr/share/themes/.
- You can find
-
How can I add emacs like keybinding in GTK applications like chromium?
- In
gtk-2add the line in yourGTKconfig file/.gtkrc-2.0.
- In
gtk-3, add the following line in yourGTKconfig file/.config/gtk-3.0/settings.ini.
- In
-
Where the emacs like keybinding in GTK applications are defined?
- In
gtk-2, the implementedemacskeybinding can be found in the file/usr/share/themes/Emacs/gtk-2.0-key/gtkrc:
# # Bindings for GtkTextView and GtkEntry # binding "gtk-emacs-text-entry" { bind "<ctrl>b" { "move-cursor" (logical-positions, -1, 0) } bind "<shift><ctrl>b" { "move-cursor" (logical-positions, -1, 1) } bind "<ctrl>f" { "move-cursor" (logical-positions, 1, 0) } bind "<shift><ctrl>f" { "move-cursor" (logical-positions, 1, 1) } bind "<alt>b" { "move-cursor" (words, -1, 0) } bind "<shift><alt>b" { "move-cursor" (words, -1, 1) } bind "<alt>f" { "move-cursor" (words, 1, 0) } bind "<shift><alt>f" { "move-cursor" (words, 1, 1) } bind "<ctrl>a" { "move-cursor" (paragraph-ends, -1, 0) } bind "<shift><ctrl>a" { "move-cursor" (paragraph-ends, -1, 1) } bind "<ctrl>e" { "move-cursor" (paragraph-ends, 1, 0) } bind "<shift><ctrl>e" { "move-cursor" (paragraph-ends, 1, 1) } bind "<ctrl>w" { "cut-clipboard" () } bind "<ctrl>y" { "paste-clipboard" () } bind "<ctrl>d" { "delete-from-cursor" (chars, 1) } bind "<alt>d" { "delete-from-cursor" (word-ends, 1) } bind "<ctrl>k" { "delete-from-cursor" (paragraph-ends, 1) } bind "<alt>backslash" { "delete-from-cursor" (whitespace, 1) } bind "<alt>space" { "delete-from-cursor" (whitespace, 1) "insert-at-cursor" (" ") } bind "<alt>KP_Space" { "delete-from-cursor" (whitespace, 1) "insert-at-cursor" (" ") } # # Some non-Emacs keybindings people are attached to # bind "<ctrl>u" { "move-cursor" (paragraph-ends, -1, 0) "delete-from-cursor" (paragraph-ends, 1) } bind "<ctrl>h" { "delete-from-cursor" (chars, -1) } bind "<ctrl>w" { "delete-from-cursor" (word-ends, -1) } } # # Bindings for GtkTextView # binding "gtk-emacs-text-view" { bind "<ctrl>p" { "move-cursor" (display-lines, -1, 0) } bind "<shift><ctrl>p" { "move-cursor" (display-lines, -1, 1) } bind "<ctrl>n" { "move-cursor" (display-lines, 1, 0) } bind "<shift><ctrl>n" { "move-cursor" (display-lines, 1, 1) } bind "<ctrl>space" { "set-anchor" () } bind "<ctrl>KP_Space" { "set-anchor" () } } # # Bindings for GtkTreeView # binding "gtk-emacs-tree-view" { bind "<ctrl>s" { "start-interactive-search" () } bind "<ctrl>f" { "move-cursor" (logical-positions, 1) } bind "<ctrl>b" { "move-cursor" (logical-positions, -1) } } # # Bindings for menus # binding "gtk-emacs-menu" { bind "<ctrl>n" { "move-current" (next) } bind "<ctrl>p" { "move-current" (prev) } bind "<ctrl>f" { "move-current" (child) } bind "<ctrl>b" { "move-current" (parent) } } class "GtkEntry" binding "gtk-emacs-text-entry" class "GtkTextView" binding "gtk-emacs-text-entry" class "GtkTextView" binding "gtk-emacs-text-view" class "GtkTreeView" binding "gtk-emacs-tree-view" class "GtkMenuShell" binding "gtk-emacs-menu"- In
gtk-3, the implementedemacskeybinding can be found in the file/usr/share/themes/Emacs/gtk-3.0/gtk-keys.css.
/* * Bindings for GtkTextView and GtkEntry */ @binding-set gtk-emacs-text-entry { bind "<ctrl>b" { "move-cursor" (logical-positions, -1, 0) }; bind "<shift><ctrl>b" { "move-cursor" (logical-positions, -1, 1) }; bind "<ctrl>f" { "move-cursor" (logical-positions, 1, 0) }; bind "<shift><ctrl>f" { "move-cursor" (logical-positions, 1, 1) }; bind "<alt>b" { "move-cursor" (words, -1, 0) }; bind "<shift><alt>b" { "move-cursor" (words, -1, 1) }; bind "<alt>f" { "move-cursor" (words, 1, 0) }; bind "<shift><alt>f" { "move-cursor" (words, 1, 1) }; bind "<ctrl>a" { "move-cursor" (paragraph-ends, -1, 0) }; bind "<shift><ctrl>a" { "move-cursor" (paragraph-ends, -1, 1) }; bind "<ctrl>e" { "move-cursor" (paragraph-ends, 1, 0) }; bind "<shift><ctrl>e" { "move-cursor" (paragraph-ends, 1, 1) }; /* bind "<ctrl>w" { "cut-clipboard" () }; */ bind "<ctrl>y" { "paste-clipboard" () }; bind "<ctrl>d" { "delete-from-cursor" (chars, 1) }; bind "<alt>d" { "delete-from-cursor" (word-ends, 1) }; bind "<ctrl>k" { "delete-from-cursor" (paragraph-ends, 1) }; bind "<alt>backslash" { "delete-from-cursor" (whitespace, 1) }; bind "<alt>space" { "delete-from-cursor" (whitespace, 1) "insert-at-cursor" (" ") }; bind "<alt>KP_Space" { "delete-from-cursor" (whitespace, 1) "insert-at-cursor" (" ") }; /* * Some non-Emacs keybindings people are attached to */ bind "<ctrl>u" { "move-cursor" (paragraph-ends, -1, 0) "delete-from-cursor" (paragraph-ends, 1) }; bind "<ctrl>h" { "delete-from-cursor" (chars, -1) }; bind "<ctrl>w" { "delete-from-cursor" (word-ends, -1) }; } /* * Bindings for GtkTextView */ @binding-set gtk-emacs-text-view { bind "<ctrl>p" { "move-cursor" (display-lines, -1, 0) }; bind "<shift><ctrl>p" { "move-cursor" (display-lines, -1, 1) }; bind "<ctrl>n" { "move-cursor" (display-lines, 1, 0) }; bind "<shift><ctrl>n" { "move-cursor" (display-lines, 1, 1) }; bind "<ctrl>space" { "set-anchor" () }; bind "<ctrl>KP_Space" { "set-anchor" () }; } /* * Bindings for GtkTreeView */ @binding-set gtk-emacs-tree-view { bind "<ctrl>s" { "start-interactive-search" () }; bind "<ctrl>f" { "move-cursor" (logical-positions, 1) }; bind "<ctrl>b" { "move-cursor" (logical-positions, -1) }; } /* * Bindings for menus */ @binding-set gtk-emacs-menu { bind "<ctrl>n" { "move-current" (next) }; bind "<ctrl>p" { "move-current" (prev) }; bind "<ctrl>f" { "move-current" (child) }; bind "<ctrl>b" { "move-current" (parent) }; } entry { -gtk-key-bindings: gtk-emacs-text-entry; } textview { -gtk-key-bindings: gtk-emacs-text-entry, gtk-emacs-text-view; } treeview { -gtk-key-bindings: gtk-emacs-tree-view; } GtkMenuShell { -gtk-key-bindings: gtk-emacs-menu; } - In
-
How to switch between GTK themes?
- Use
gtk-theme-switchcommand. - To install it, run the command:
- Use
-
How to modify text size of GTK applications like chromium?
- In
gtk-3, add the following line in yourGTKconfig file/.config/gtk-3.0/settings.ini.
- In
-
How can I find GTK resources?
-
How can I test/debug components of an application that uses GTK for the UI?
- Use
GTK inspector. - See GTK inspector on gnome.org.
- To do so, run the command:
- To install
GTK inspector, you have to install the package
libgtk-3-dev, to do so, run the command: - Use
Xft¶
-
How can I scale up my screen view?
- Use
~/.Xresourcesfile and/orxrdbcommand. - Many desktop environments use
xrdbto load~/.Xresourcesfiles on session startup to initialize the resource database. - See Xresources and HiDPX archlinux.
- If you want to scale up what you see on your screen, saying that your
dpi(dot per inch) is equal to96and you want to raise up to144 dpi(dpi must be a multiple of 96), you can add this lineXft.dpi: 144to the file~/.Xresources, and run the command:
- If you want to check the currently loaded resources, run the command:
- To see the default settings of your installed
X11 apps, look in/etc/X11/app-defaults/.
- Use
i3 window manager¶
-
How can I debug i3status and i3blocks config files?
- Use
i3statusori3blokscommands. i3blocks: a flexible scheduler for your i3bar blocks.i3status: generates a status line for i3bar.- If you want to debug
i3statusconfig file, run the command:
- If you want to debug
i3blocksconfig file, run the command:
- Use
-
How can I know the key name use by i3 config file for a specific key?
- Use
xevcommand. xev: print contents of X events.- If you want to get information about a specific key on your keyboard, that you can use in your i3 config file, run the command
xevin your terminal, a (Event tester) window popup, then press the key from which you want to get information. For instance, pressing thespacekey on my keyboard, I get this following output wherekeycode 65 (keysym 0x20, space)is the interesting part and the name to use in i3 config file isspace.
- Use
Gnome¶
-
How can I reset all gnome settings?
- Use
gnome-tweakscommand. gnome-tweaks: customize your Ubuntu Gnome desktop.- If you want to reset all gnome desktop settings to default, launch
gnome-tweakand in its gnome menu clickReset to Defaults. - if you want to install
gnome-tweaks, run the command:
- Use
-
How can I modify the text size on Gnome desktop?
- Use
gsettingscommand. gsettings: GSettings configuration tool.- See example find and change the screen dpi on askubuntu.
- If you want to change the
dpiin Ubuntu Gnome desktop, you can't because it's hard coded to a value of96 dpi. But you can change the text size of the applications. For instance, to increase the text size by1.2(of the hard coded96 dpi, i.e.1.2*96), set to1.2the value of the keytext-scaling-factorof the shemaorg.gnome.desktop.interfacerun the command:
- If you want to go back to the default text size, reset the value of key
text-scaling-factor. To do so, run the command:
- Use
fonts¶
-
How can I list all available font on my system?
- Use
fc-listcommand. fc-list: list available fonts.- If you want to list all available font, run the command:
- Note: the font
symbolaallow you to print allunicodecharacter.
- Use
Users and groups¶
Sudo and Root¶
-
How can I switch to root user?
- Use
sucommand. su: change user ID or become superuser.- See su command by linuxize.
- If you want to run an interactive shell as root when root's password is already defined, run the command:
- If you want to run an interactive shell as root when root's password is not defined, run the command:
- Use
-
How can I add a user to the sudo group?
- Use
usermodcommand. usermod: modify a user account.- If you want to add the user
jimto the groupsudo, run the command:
- Use
-
How can I remove a user from the sudo group?
- Use
delusercommand. deluser: remove a user from the system.- If you want to remove the user
jimfrom the groupsudo, run the command:
- Use
-
How can I modify the sudo password timeout?
- Use
visudocommand. visudo: edit the sudoers file in a safe fashion.- See sudo password timeout by maketecheasier.
- If you want to change the default (15mins) sudo password timeout to
30mins, change the lineDefaults env_resetto the lineDefaults env_reset, timestamp_timeout=30in the file/etc/sudoers, not directly, but by running the command:
- If you want to check the existing sudoers file for syntax errors and mode, run the command:
- If you want that the system prompt you for password everytime you use
sudo, set the sudo password timeout to0, by changing the lineDefaults env_resetto the lineDefaults env_reset, timestamp_timeout=0in the file/etc/sudoers, not directly, but by running the command:
- Use
Groups¶
-
How can I list all groups a user is a member of?
- Use
groupscommand. groups: print the groups a user is in.- See how to list groups in linux by linuxize.
- If you want to list the groups the user
jimis a member of, run the command:
- Use
-
How can I list all groups present on the system?
- Use
getentcommand. getent: get entries from Name Service Switch libraries.- See how to list groups in linux by linuxize.
- If you want to list all groups present on the system, run the command:
- Use
Passwords¶
-
How can I enable root user account?
- Use
passwdcommand. passwd: change user password.- See root user account by linuxize.
- If you want to enable the root user account and you have the sudo privileges, run the command:
- If you want to disable the root user account and you have the sudo privileges, run the command:
- Use
-
How can I encrypt a password that can be used in the /etc/shadow file?
- Use
mkpasswdcommand. mkpasswd: Encrypts the given password with the crypt(3) libc function using the given salt.- See examples generate a password in shadow file on stackexchange.
- If you want to encrypt a password into
SHA 512hash with the given saltsaltsalt, run the command (and type your password):
- If you want to encrypt a password into
SHA 512hash without specifying the salt (a random one will be provide), run the command (and type your password):
- I you want to list all the encryption methods, run the command:
- I you want to install the
mkpasswdcommand, you have to install the packagewhois. To do so, run the command:
- Use
UID¶
-
How can I modify the UID of an user on linux?
- Use
usermodcommand. usermod: modify a user account.- See usermod command example by linuxize.
- If you want to modify the UID of the user
tonyto the number1050for instance, run the command:
- If you want to print the UID of the user
tony, run the command:
- Use
Miscellaneous¶
-
What adduser command is for?
adduser: add a user to the system.
-
What addgroup command is for?
addgroup: add a group to the system.
-
What delgroup command is for?
delgroup: remove a user from the system.
-
What chown command is for?
chown: change file owner and group.
-
What chgrp command is for?
chgrp: change group ownership.
X window system¶
Get X information¶
-
How can I print all X information on a selected window?
- Use
xwininfocommand. xwininfo: window information utility for X.- If you want to to print all X information on a selected window, run the command
xwininfo -alland click on the window you want the information from:
- Use
X and i3¶
-
How can I kill a window that I can't kill with i3?
- Use
xkillcommand. xkill: kill a client by its X resource.- If you want to kill a window that doesn't respond, run the command
xkilland click on the window you want to kill:
- Use
-
How can I get the instance and class of X window that are used with i3?
- Use
xpropcommand. xprop: property displayer for X.- If you want to get the instance and class of an X window in order to use them with
i3, run the commandxpropand click on the window you want the information from and inspect the line starting byWM_CLASS(STRING):.
- For example with a window running
chromium-browserapplication, I get the following line wherechromium-browseris the instance, secondChromium-browseris the class:
- Use
Multiple monitors¶
-
How can I display a monitor on the left of the other monitor?
- Use
xrandrcommand. xrandr: set the size, orientation and/or reflection of the outputs for a screen.- If you want to display the monitor
DP-2on the left of the monitoreDP-1, run the command:
- If you want to get the names of the outputs you want to display with
xrandr, run the commandxrandrand seek for lines like those ones (in my caseeDP-1andDP-2are the monitors I want to display side by side):
- Use
Miscellaneous¶
-
What xbindkey command is for?
xbindkey: a grabbing keys program for X.
-
What xmodmap command is for?
xmodmap: utility for modifying keymaps and pointer button mappings in X.
-
How can I list the open windows?
- Use
wmctrlcommand. wmctrl: interact with a EWMH/NetWM compatible X Window Manager.- If you want to list the windows being managed by the window manager, run the command:
- Use
-
How can I simulate a key plus mouse click from commande line?
- Use
xdotoolcommand. xdotool: command-line X11 automation tool.- See simultaneous key + mouse click on stackoverflow and xdotool man page on ubuntu.
- If you want to simulate
alt+mouse-1simultaneously from command line, run the command:
- Use
-
How can I get current mouse coordinates from bash?
- Use
xdotoolcommand. xdotool: command-line X11 automation tool.- See get current mouse coordinates on stackoverflow.
- If you want to get the current mouse coordinates location from command line, run the command:
- If you want to get the current mouse coordinates location and set the variables
XandYto hold respectively the horizontal and vertical position, run the command:
- Use
-
Some use cases of xdotool command
- If I run the command
xdotool getactivewindowI get27263352. - If I run the command
xdotool getactivewindow getwindownamefrom my terminal I getTerminal. - If I run the command
xdotool getactivewindow | xprop -id 27263352 WM_CLASS, I got this output:
- If I run the command
Autokey¶
-
How can I install autokey?
- Use
gitandpip3commands. pip3: a tool for installing and managing Python packages.git: the stupid content tracker.- See Installing wiki and PyGObject getting started documentation.
- If you want to install
autokeywithpip3, clone the repository, install dependencies that are not insetup.pyfiles aswheel,dbus-pythonandPyGObject, and, installautokeyfrom the cloned repository. We do every needed steps in apythonvirtual environment. Finally, we make a symlink ofautokey-gtkexecutable in~/.local/bin/directory to be able to useautokeywidely without activating thepythonvirtual environment. To do so, we run the commands:
$ git clone https://github.com/autokey/autokey.git $ cd autokey $ python3 -m venv ~/.py-venv/autokey/ $ source ~/.py-venv/autokey/bin/activate (autokey) $ pip3 install wheel dbus-python (autokey) $ sudo apt install libgirepository1.0-dev gcc \ libcairo2-dev pkg-config python3-dev gir1.2-gtk-3.0 \ # for PyGObject (autokey) $ pip3 install pycairo (autokey) $ pip3 install PyGObject (autokey) $ pip3 install . (autokey) $ ln -s ~/.py-venv/autokey/bin/autokey-gtk \ ~/.local/bin/autokey-gtk (autokey) $ deactivate- To start
autokeywith the GUI, run the command:
- To start
autokeyin background, run the commandautokey-gtkwithout any command line arguments:
- Use
-
How can I have autokey run in background and start at login to my linux session?
- Use
systemctl,autokey-gtkcommands andi3directiveexec. systemctl: control thesystemdsystem andservice manager.exec: executes bash commands in thei3wmconfig file~/.config/i3/config.- If you want
autokeyto start in background when you login to your linux session, use thesystemdand add it the systemd unit file~/.config/systemd/user/autokey.service:
[Unit] Description=Desktop automation for Linux and X11 Documentation=https://github.com/autokey/autokey [Service] Type=notify ExecStart=/home/tony/.local/bin/autokey-gtk ExecStop=/bin/systemctl show --property MainPID --value emacs --user | kill Restart=always [Install] WantedBy=default.target- Then you have to enable the service and start it. To do so, run the command:
- Use
-
Where are stored autokey config files?
autokeyconfig files are in~/.config/autokey/directory.
-
Where can I find autokey ressources?
- autokey github repository and the
autokeywiki pages Installing, FAQ, Scripting, API-Examples, Special-Keys.
- autokey github repository and the