Consolidating the foundation of Linux, linux tips

Linux Tips

Record the commands that I use more frequently in the form of a memo, and will continue to increase according to usage in the future.

# Commands Comment
shopt -s histappend Sync history in multiple terminal
shopt -s cdspell Correct the typos of cd
export, mount, set Check the defined value
&& run the following command only when the previous is successful
; run the following command regardless of success of first one
which <cmd>,file <file>,command -V <cmd> Examine executable file
pushd [-n] <dir>,dirs -l -v
popd [-n] [+N | -N]
Manage directories with stack
dmesg -w Wait for new messages
less -N +F view logfiles and number the lines
ls -ld list directories themselves, not their contents
flock -xn "/tmp/f.lock" c "filename.sh" Lock ‘filename.sh’ with flock
LANG=C stat file Display file status in English
time <cmd> run and summarize system resource usage of <cmd>
tac conatenate and print files in reverse,`cat -n file
r repeat the previous cmd
bash -i script Make the shell interactive
shred -u <file> Overwrite file and remove it, better way to delete than ‘rm -rf file’
nl number lines of contents, ls | nl -s ":" -w 1
split <prefix>, cat preifx* > file split , merge file
killall kill processes by name
fc fix and execute commands from history in editor
Ctrl-X-E execute commands in editor
touch -t 2104250101 <file> Change the timestamp of file
touch -r <file1> <file2> file1’s times instead of current time
Ctrl-D Stop current cmd or bash …
dd Convert and copy a file, ‘man dd’
hexdump, hd -C <binary> ASCII, decimal, hexadecimal, octal dump
select-editor Choose the default editor
ranger man ranger, ‘?’ in ranger
sl -F A funny command
apropos search the manual page names and descriptions,eg:apropos "list directory"
echo > /dev/<udp or tcp>/<server ip>/<port> Check net
2>&1 2 means stderr, &1 means stdout
bind -p to check the shortcut
Ctrl - _ undo
ls -alrRt -r: reverse, -R: Recursive
cp -ar(R)pv -p keep mode,ownership,timestamps; -r equals -R
tail -nf Output the last n line appended data as the file grows
more/less File perusal filter for crt viewing, less opposite of more
wc Print newline, word, and byte couts for each file,-l line
tar -czf/cjf -j bzip2, .tar.bz2, max compression; -z gzip, tar.gz
whatis <cmd> display one-line manual page description
Move: Ctrl - t swap two characters
!!, !-1 Refer to the previous command
!* or Alt+. Refer to the previous parameters
Ctrl-R Search command from history, doulbe to search next
ALT - t swap two words
ALT - . prints last argument from previous command
Ctrl - x - * expand glob/star
Ctrl - arrows move forward a word
ALT - f move forward a word
ALT - b move backward a word
Alt-Backspace delete forward a word
Ctrl - x , Ctrl - e Edit command with editor
Ctrl - e move cursor to end
Ctrl - a move cursor to start
Ctrl - P, N, B, F Move previous, next, backward a char, Forward a char
Ctrl - xx move to the opposite end of the line
Ctrl - u cuts everything before the cursor
Ctrl - k cuts everything after the cursor
Ctrl - y pastes from the buffer
Ctrl-W cuts one word before the cursor
Ctrl - L clears screen
Ctrl - shift - c/v copy/paste into terminal
Ctrl-Insert, Shift-Insert copy/paste
Ctrl-Z,fg, bg Pasue the current process, foregroud/backgroud it
lscpu, cat /proc/cpuinfo information about cpu
ps [-eLf] Get information about process including threads
pstree -p, -a Get information about process with tree
nice, renice alter priority of process
ulimit -a get and set user limits
free -m, cat /proc/meminfo information about memory
lsblk, cat /proc/partitions formation harddisk and partition
df -h infromation about partition
mount | column -t status about mounted partition
hostname show or set the hostname
w show who is logged on and what they are doing
id print user and group IDs
last show a listing of last logged in users
cut -d: -f1 /etc/passwd show all users
cut -d: -f1 /etc/group show all groups
chkconfig ?list | grep on show all on system service
uptime show operation time,user number,load
ls, du, ln, rmdir, rm, cp, mv, cat, tail, less, grep, find
htop, top display processes, 1 - extend top information
sudo dpkg --get-selections | grep linux-image
cat /proc/version
grep menuentry /boot/grub/grub.cfg
Kernel version
apt-cache search package query the APT cache
apt update
uname -a , -r
cat /proc/version
cat /etc/issue
lsb_release -a
print system information
apt upgrade
apt autoremove
apt install update-manager-core
flameshot``flameshot gui Capture screen
Ctrl + Alt + F1/../F7 Enter or quit terminal
killall <nautilus> Kill file manager
pkill Xorg, restart lightdm Log out
$(date "+%Y%m%d-%H%M%S") Get the current system time
sshfs user@ip:/dir name mount sshfs
fuser mount -u name unmount sshfs , umount -l name for busy error
Supper Mode:
rename 's/old_name/new-name/' *.txt rename files
cp | mv test.sh{,.bk}, mv test{.sh,} rename quickly, remove suffix
find . -type f -exec cat {} + find and cat flies recursively
find . -type f -exec ls {} + find . type f -exec ls {} \;
find ./ -type f -exec sed -i 's/old/new/g' {} \; Replace all old in the current directory with new
swapon --show Check swap space
swapoff -a
dd if=/dev/zero of=/swapfile bs=1G count=8
mkswap /swapfile
swapon /swapfile
grep SwapTotal /proc/meminfo
Extend swap space to 8G
free -h
top,htop Interactive process viewer
vmstat Report virtual memory statistics
pmap Report memory map of a process
kmemleak Embeded memeory analysis tool
at <time> + Execute some commands on
cron Daemon to execute scheduled commands,/etc/cron.*, /etc/ancrontab
crontab -e,crontab -l Edit, list the scheduled commands
anacron run commands periodically
/var/cache/apt/archives/ All archived debs
hexedit,hex Hexdump
xxd Make a hexdump or do the reverse
strings -a -a Scan the whole file
cpio -if Copy files to and from archives
lspci | grep -i 'eth' Information about eth
ifconfig -a information about all net port
ethtool eth0 Details of eth0
ping <remote host address>
ssh, sshfs, scp remote login, mount, copy
iptables -L information about firewall
service iptables status status about firewall
service iptables stop Close firewall
route -n show the IP routing table
netstat -lntp,-antp,-s,-at,-au,-lt information about port,conection,…
addgroup lee vboxusers Make virtual box recognize USB
*/?/[xyz]/[^xyz]/[!xyz]/[a~z] Regular expression
make > /dev/null
while true; do adb shell cat /proc/interrupts | grep kgsl; sleep 0.5; done
adb shell cd sdcard nohup watch -n 5 'cat /sys/bus/cpu/devices/cpu0/online >> online.log' &
readelf -h vmlinux
nm List symbols from object files, `nm -n vmlinux
ldd Print shared object dependencies, list dynamic dependencies, likes xxx.so
objdump Display information from object files, disassembler
ndisasm -u The Netwide Disassembler, an 80x86 binary file disassembler
c++filt Demangle C++ and Java symbols
hexdump, hd ASCII, decimal, hexadecimal, octal dump, hexdump -s 0x18408a8 -n 1024 vmlinux
file Determine file type, file *.img, *.mbn, *.bin ...
readelf Displays information about ELF files,readelf -hlSVAI vmlinux
objdump Display information from object(files) xxx-objdump -afphG vmlinux
objcopy Copy and tranlate objects, eg:Generate zImage with vimlinux
[android-unpackbootimg](https://github.com/anestisb/android-unpackbootimg) -i boot.img -o boot_img/
python3 [extract-dtb.py](https://github.com/PabloCastellano/extract-dtb) kernel Get dts from kernel
dtc -I dtb -O dts ... -o ... Disassembler
od Dump files in octal and other formats
vim -b file + :%!xxd -u + :%!xxd -r Edit ELF file, -u to hex, -r to binary
ltrace A library call tracer
strace Trace system calls and signals
gdb The GNU debugger
strip Discard symbols from object files
lsof List open files opened by processes
fuser Identify processes using files or sockets
/proc Process information pseudo-filesystem
ELF Format of Executable and Linking Format(ELF) files

Add default CDPATH

export CDPATH='$CDPATH:...'
export CDPATH='~:/var/log:/etc'
lee@lee-pc:~$ cd fonts

Modify user

sudo chown -R $(whoami) file

Get IP and Netmask

# IP
hostname -I
ifconfig | grep -Eo 'inet (addr:)?([0-9]*\.){3}[0-9]*' | grep -Eo '([0-9]*\.){3}[0-9]*' | grep -v ''

# Netmask
ifconfig | sed -En 's/;s/.*netmask (addr:)?(([0-9]*\.){3}[0-9]*).*/\2/p'

# Only wlan0
ifconfig wlan0 | ...


ls /proc/device-tree/soc/leds-gpios/