Things on this page are fragmentary and immature notes/thoughts of the author. Please read with your own judgement!
-
command-not-found - Suggest installation of packages in interactive bash sessions
-
nohup, disown
-
nautilus-open-terminal
Configuration
https://github.com/thoughtbot/til/blob/master/bash/bash_profile_vs_bashrc.md#:~:text=bashrc%20is%20sourced%20on%20every,with%20the%20%2D%2Dlogin%20option.&text=bash_profile%20is%20great%20for%20commands%20that%20should%20run%20only%20once%20and%20
-
Debian does not read in the
.profile
file on start-up of X windows. To solve this problem, you can source in.profile
in in the.xsessionrc
file. Ubuntu and Mint does read in the.profile
file on start-up of X windows. -
You'd better use robust configuration files. If an error is encountered in the
.xsessionrc
file, the desktop environment might fail to start.
Media
- Empty CD tray, cannot eject by pressing the bussion, an interesting trick to eject CD: try to amount the CD eject the tray
Cool Command
-
~
is recognized as the home directory of the current user in shell only. You'd better not use it in other places. Whenever the home directory of the current user is needed, you can always use$HOME
instead. -
Check whether a system is 32 bit or 64 bit
getconf LONG_BIT
-
all parameters:
$@
(what about$*
?) -
Be careful about
.
when using regular expressions (e.g., sed), this is really a general problem -
the trick of "--" end of command after which only positional arguments are accepted
-
variable substituation makes things very interesting in shell, for example awk and so on ..., always be careful if you use "$" and so on....
-
process of substitution <() >(), pipe ...
-
variables defined in bash function won't polute the global environment and local make the variables visible only to the scope of the function
-
prefer dot rather than source for sourcing script, because source is only for bash
-
executable files: if in $HOME/bin, prefer symbolic links, if for general uer, prefer to make a global copy, don't use symbolic link in /usr/bin pointing to your own files, you might change your file permissions and mess up the program
-
If an environment path contains spaces, you have to quote it with double/single quotations marks in order to make shell commands work correctly.
top
- top command Unit of the Time+ column: minutes:seconds.hundredths
ls
-
ls -d */
displays all directories in the current folder -
ls /home/*
in bash, don't use it, instead use(/home/*)
directlyls -d "/home/dclong/btsync/backup/tbp_"*
instead of
ls -d "/home/dclong/btsync/backup/tbp_*"
it seems to me that if you wildcard, then full names are returned, otherwise, short names are returned.
-
By default, the year for files older than 6 month are not displayed. You can force
ls
to show full time information byls -lh --full-time
.
rsync
rsync
,/
at the end is important, this decides whether the folder is copy or its content is copied, unison is similar ... This is the same for some other commands like Amazon S3, etc. Also if you want to exclude an folder fromrsync
, you have to include the trailing slash.
alias
-
it seems alias has lots of limitations ... only use single quotation mark, causes lots of problem YOU ACTUALLY CAN USE DOUBLE QUTOES
-
If you define an alias of a path (especially a Windows path in Cygwin, MobaXterm, etc.), you have to quote the path in commands, generally speaking. For example, if the alias of path you define is
p
(which contains spaces), you can usecd "$p"
to change working directory to it. Note that you cannot omit the quotes.
Filesystem
cp
-
always use
cp
with-i
option! -
cp -a
might not be a good idea sometimes, e.g., when in Cygwin/MobaXterm, because you might lose file permissions due to unmatch of users -
Both cp and mv overwrite the destination file if is is already exists. cp does not overwrite existing destination directory but instead acts as merging contents of directories. mv refuses to rename a directory to a destination directory if the destination directory is not empty.
It is suggested that you use rsync as much as possible as an alternative to cp.
# copy sub files/folders excluding hidden files/folders
cp a/* b
# copy sub files/folders including hidden files/folders
cp a/. b
mv
- always use interactive version of
mv
andcp
... and always usetrash
instead ofrm
du & dust
grep & ripgrep
ps & procs
cat & bat
bat
is a much better alternative to cat
.
rm & rip
rm
is dangerous.
It is suggested that you use the rip
command (implemented in Rust)
as much as possible.
-
Remove a file whose name starts with
--
and has special characters.rm ./'--exclude.!sync'
-
Get file name without extension.
basename -s .txt f.txt
-
Create a temp file.
mktemp -tu file.XXXXX
mktemp
is preferred overtempfile
.
gpasswd
-
gpasswd
is great for adding and removing user to/from groupsgpasswd -d user group
type
type fun_name
shows source code of bash functions or usedeclare -f
type command
path of command
mount
-
check if a path is a mount point
if [ $(stat -fc%t:%T "mnt/wd") != $(stat -fc%t:%T "mnt/wd/..") ]; then echo "mounted"; else echo "not mounted"; fi
-
mount as normal user instead of root
mount -o uid=dclong ...
sudo
-
I think it's better to use sudo inside bash script instead of prefix sudo to script names to invoke them. The reason is that you use sudo only when needed.
-
sudo alias
alias sudo='sudo '
the space is necessary
real absolute path
-
realpath: absolute path
-
absolute path
readlink path
random
-
Generate a random number.
random 0 - 32767 uniform
Power Management
- Show battery/power information
acpi -bi
Administration
-
shutdown, poweroff, halt It seems these commands have different meanings on different Linux distributions. The actually case is really complicated. It depends on different Linux distributions and also the services running on laptops. If a service fails to stop, the shutdown process might freeze at the logo page.
halt -p
It is suggested that you always use the
poweroff
command to shutdown (and poweroff) your computer. You should close all programs before you logout or shutdown your computer. -
to change time zone using command sudo dpkg-reconfigure tzdata
-
ssh -X
X11 forwarding, make it possible for you to use GUI applications on server
Task
at
command run in backgroud, so do not use it to do things that have to communicate with stdin stdout ...
Archive
-
DO NOT USE the 7-zip format for backup purpose on Linux/Unix because 7-zip does not store the owner/group of the file.
-
difference between 7za and 7z?
UI
-
use syndaemon -dti 0.5 to disable touchpad while typing. You have to have
Option SHMConfig "on"
orOption SHMConfig "true"
in the synaptics configuration file. -
Sometimes when you login into a server, it freezes after printing the welcome message. You can press
CTRL + C
to kill running process and it might help you bring the terminal back. -
Linux release specific information
lsb_release -a
the lsb-release package
Process
-
Use
Ctrl + Z
to pause a process and usebg
to send it to background to run. Usefg
to bring a background process to foreground. -
ldd --version
check glibc version -
You can use
ps aux | grep -i user_id
to list your jobs andkill -9 process_id
to kill a process. On platform Load Sharing Facility (LSF), you probably don't have access tokill
unless are a privileged user. Instead of usingps
andkill
, you can usebjobs
to show your jobs andbkill process_id
to kill your process. -
when defining alias, do not use tailing
/
in paths unless you have a good reason for that ... -
file to check link cannot use trailing
/
-
what have caused the blog difference issue?, line terminators
-
comm: make sure you don't screw up by invisible white spaces!!!, intersection/inner join, left join, right join (add keywords to your blog)
-
type of disk
df -T
CPU Stats
UUID in Bash
uuidgen
need the libuuid library which is part of util-linux
id -un
is a better alternative thanwhoami
.
Timezone
Linux
cat /etc/timezone
Mac https://slaptijack.com/system-administration/set-mac-os-x-time-zone-from-the-command-line/
References
- Good Terminal Apps
- Terminal Multiplexers
- http://www.legendu.net/misc/blog/programming-in-shell
- http://www.legendu.net/misc/blog/terminator-tips/
- http://www.legendu.net/misc/blog/terminology-tips/
- Rolo
- Five Really Handy Google Command Line Tricks