Skip to content

keineahnung2345/linux-commands

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

linux-commands

Some useful linux commands

differences of /etc/profile, ~/.bash_profile, ~/.bashrc

How can I set a single .bashrc file for several users?

Output of man bash(only a part):

FILES
       /bin/bash
              The bash executable
       /etc/profile
              The systemwide initialization file, executed for login shells
       /etc/bash.bashrc
              The systemwide per-interactive-shell startup file
       /etc/bash.bash.logout
              The systemwide login shell cleanup file, executed when a login shell exits
       ~/.bash_profile
              The personal initialization file, executed for login shells
       ~/.bashrc
              The individual per-interactive-shell startup file
       ~/.bash_logout
              The individual login shell cleanup file, executed when a login shell exits
       ~/.inputrc
              Individual readline initialization file

check boot/shutdown log

[Linux] 查詢 Linux 的 shutdown/reboot 記錄

last reboot      # check boot log
last -x shutdown # check shutdown log

switch to root environment(Ubuntu)

Why does su fail with “authentication error”? [duplicate]

What are the differences between “su”, “sudo -s”, “sudo -i”, “sudo su”?

sudo -s # ~/.bashrc is respected
sudo -i # ~/.bashrc is ignored

change password

passwd

Then key in original password and then key in new password twice.

sudo, given password

sudo 自動帶入 密碼

echo '<password>' | sudo -S <command>

restart bash

Restart bash from terminal without restarting the terminal application (mac)?

exec bash

list all commands recognized by linux shell

compgen -c 

check manual of a command

man <other-command>

find out where <other-command> exists

which <other-command>

or

whereis <other-command> #used to locate binary, souce for a command

check system information(either of one)

hostnamectl
uname -a
cat /etc/*-release

check ubuntu system information(version)

lsb_release -a

This can be installed by:

apt-get install -y lsb-release

check ubuntu architecture

Easy command line method to determine specific ARM architecture string?

dpkg --print-architecture
# amd64

check CentOS/RHEL OS update level(version)

one of them:

cat /etc/redhat-release
cat /etc/centos-release
cat /etc/system-release
cat /etc/os-release
rpm -qf /etc/redhat-release

list all users

How to List Users in Linux

less /etc/passwd

check cuda version

nvcc --version

or

nvcc -V

When nvcc cannot be found:

cat /usr/local/cuda/version.txt

check cudnn version

cat /usr/local/cuda/include/cudnn.h | grep CUDNN_MAJOR -A 2

check if your machine is physical or virtual(either of one)

hostnamectl status (for CentOS7)
systemd-detect-virt
facter | grep virtual (suitable for CentOS6)

check cpu specification

lscpu

or:

cat /proc/cpuinfo

check cpu microarchitecture

how to find out intel architecture family from command line

gcc -march=native -Q --help=target|grep march

Sample output:

  -march=                     		broadwell

get logical core count

How to know number of cores of a system in Linux?

echo $(nproc) # 4

The equivalent for Mac(nproc is not a command on Mac):

sysctl -n hw.logicalcpu

check external devices' information

lsusb

check your gpu driver version

nvidia-smi

If it says:

NVIDIA-SMI couldn't find libnvidia-ml.so library in your system. Please make sure that the NVIDIA Display Driver is properly installed and present in your system.
Please also try adding directory that contains libnvidia-ml.so to your system PATH.

Following NVIDIA-SMI couldn't find libnvidia-ml.so library, add the following line to your ~/.bashrc:

export LD_PRELOAD=/usr/lib/nvidia-418/libnvidia-ml.so

And then source ~/.bashrc.

monitor GPU usage

nvidia-smi

nvidia-smi --loop=1
nvidia-smi -l=1

or

nvidia-smi --loop-ms=500
nvidia-smi -lms=500

or

watch -n0 nvidia-smi

gpustat

How to measure GPU usage?

wookayin/gpustat

gpustat -cp

Note: -c for show command, -p for show pid

Install it by pip.

check your gpu specification

nvidia-smi -q

a enhanced nvidia-smi, showing processes's info

First install peci1/nvidia-htop, and then:

nvidia-htop.py -c -l 50

This will show the processes' PID, USER, GPU, MEM, %CPU, %MEM, TIME, COMMAND.

check gpu memory size in detail

How to find out shared memory and global memory size of GPU?

# cd <cuda_installatino_dir>/samples/1_Utilities/deviceQuery
cd /usr/local/cuda/samples/1_Utilities/deviceQuery
# compile the cpp file
make
# run deviceQuery
./deviceQuery

Result:

./deviceQuery Starting...

 CUDA Device Query (Runtime API) version (CUDART static linking)

Detected 1 CUDA Capable device(s)

Device 0: "GeForce GTX 1080"
  CUDA Driver Version / Runtime Version          10.1 / 10.1
  CUDA Capability Major/Minor version number:    6.1
  Total amount of global memory:                 8116 MBytes (8510701568 bytes)
  (20) Multiprocessors, (128) CUDA Cores/MP:     2560 CUDA Cores
  GPU Max Clock rate:                            1835 MHz (1.84 GHz)
  Memory Clock rate:                             5005 Mhz
  Memory Bus Width:                              256-bit
  L2 Cache Size:                                 2097152 bytes
  Maximum Texture Dimension Size (x,y,z)         1D=(131072), 2D=(131072, 65536), 3D=(16384, 16384, 16384)
  Maximum Layered 1D Texture Size, (num) layers  1D=(32768), 2048 layers
  Maximum Layered 2D Texture Size, (num) layers  2D=(32768, 32768), 2048 layers
  Total amount of constant memory:               65536 bytes
  Total amount of shared memory per block:       49152 bytes
  Total number of registers available per block: 65536
  Warp size:                                     32
  Maximum number of threads per multiprocessor:  2048
  Maximum number of threads per block:           1024
  Max dimension size of a thread block (x,y,z): (1024, 1024, 64)
  Max dimension size of a grid size    (x,y,z): (2147483647, 65535, 65535)
  Maximum memory pitch:                          2147483647 bytes
  Texture alignment:                             512 bytes
  Concurrent copy and kernel execution:          Yes with 2 copy engine(s)
  Run time limit on kernels:                     Yes
  Integrated GPU sharing Host Memory:            No
  Support host page-locked memory mapping:       Yes
  Alignment requirement for Surfaces:            Yes
  Device has ECC support:                        Disabled
  Device supports Unified Addressing (UVA):      Yes
  Device supports Compute Preemption:            Yes
  Supports Cooperative Kernel Launch:            Yes
  Supports MultiDevice Co-op Kernel Launch:      Yes
  Device PCI Domain ID / Bus ID / location ID:   0 / 1 / 0
  Compute Mode:
     < Default (multiple host threads can use ::cudaSetDevice() with device simultaneously) >

deviceQuery, CUDA Driver = CUDART, CUDA Driver Version = 10.1, CUDA Runtime Version = 10.1, NumDevs = 1
Result = PASS

solve NVIDIA NVML Driver/library version mismatch

NVIDIA NVML Driver/library version mismatch

lsmod | grep nvidia
rmmod <mod-name1> #follow the order from the last command
rmmod <mod-name2>
# rmmod ...
nvidia-smi # should work now

kill all processing using GPU

How do I kill all the computer processes shown in nvidia-smi?

kill -9 $(nvidia-smi | sed -n 's/|\s*[0-9]*\s*\([0-9]*\)\s*.*/\1/p' | sort | uniq | sed '/^$/d')

cuda executable profiling

nvprof ./<cuda_executable>

check Intel GPU Info

glxinfo | grep -i device

Sample output:

    Device: Mesa DRI Intel(R) Iris Plus Graphics 650 (Kaby Lake GT3e)  (0x5927)

Note that it should be used on a computer with GUI, otherwise it gives the following error:

Error: unable to open display

Another option(can be used when there is no display): How do I find version of Intel graphics card drivers installed?

lspci -k | grep -EA3 'VGA|3D|Display'

check Intel GPU usage

Ref: Check your intel gpu usage via commandline

intel_gpu_top

check OpenCL version

clinfo --version

Sample output:

clinfo version 2.1.16.01.12

find process id and then kill it

kill -9 `pgrep <your_exec_file_name>`

shutdown the machine

shutdown now

reboot the machine

shutdown -r now

backup your system

reference: https://help.ubuntu.com/community/BackupYourSystem/TAR

cd /
tar -cvpzf backup.tar.gz \
--exclude=/backup.tar.gz \
--exclude=/proc \
--exclude=/tmp \
--exclude=/mnt \
--exclude=/dev \
--exclude=/sys \
--exclude=/run \
--exclude=/media \
--exclude=/var/log \
--exclude=/var/cache/apt/archives \
--exclude=/usr/src/linux-headers* \
--exclude=/home/*/.gvfs \
--exclude=/home/*/.cache \
--exclude=/home/*/.local/share/Trash /

measure running time of a command

time <other-command>

Output:

real	1m4.743s
user	0m54.368s
sys	0m16.128s

list all locale

locale -a

generate locale

sudo locale-gen zh_TW.UTF-8

check current date and time

date

Write into log file named by date:

echo "abc" > $(date "+%Y-%m-%d-%H-%M-%S"log.txt)

get time in seconds since epoch

Get current time in seconds since the Epoch on Linux, Bash

date +%s

This is same as the following in Python:

import time
time.time()

change timezone

cd /usr/share/zoneinfo
tzselect # and then follow the instructions

To make this change pernament, add the following line in the last of ~/.profile:

TZ='Asia/Taipei'; export TZ

sleep

What does the sleep command do in Linux?

sleep <number_of_seconds>

check system log around the date <yyyymmdd>(It records the boot and shutdown histories)

cat /var/log/messages-<yyyymmdd> | less

make the output of a command be a command and then execute it

eval $(<command-whose-output-is-the-commands-you-want-to-execute>)

show all environment variables

printenv

find an environment varialbe by name

env | grep -i "<env_var_name>"

export an environment variable

export <env_var>="<value>"

Note that there should not be any spaces before or after '=', or it throws bad variable name or bash: export: `=': not a valid identifier.

Ref: How do I add environment variables?

concat string to an environment variable

echo $<env_var>"str_to_concat"

append current directory to the environment variable $PATH

export PATH=$PATH:`pwd`

To make it work forever, add this line into the bottom of ~/.bashrc and then source ~/.bashrc.

remove the environment variable <ENV_VAR_NAME>

unset <ENV_VAR_NAME>

get host name

hostname

get ipaddress

ifconfig | grep -A 1 '<network-interface-name>' | tail -1 | tr -s ' ' | cut -d' ' -f 3 | cut -d':' -f 2

<network-card-name> could be enp1s0f1, eno1 or eth0 depending on your machine.

ip route get 1 | awk '{print $NF;exit}'

or

hostname -i

or

hostname -I | cut -d' ' -f1

To get external ip address:

How to Find the Public IP Address on a Linux System

host myip.opendns.com resolver1.opendns.com | grep "myip.opendns.com has" | awk '{print $4}'

or

dig +short myip.opendns.com @resolver1.opendns.com

restart a specific network interface

How to Restart Network Interface in Linux

using ifdown:

ifdown <network-interface-name> && ifup <network-interface-name>

using ifconfig (cannot reconnect for me):

ifconfig <network-interface-name> down && ifconfig <network-interface-name> up

For Ubuntu, also:

sudo /etc/init.d/networking restart

or

sudo systemctl restart networking

check unused ip address

Any way to find unused IP addresses on my network?

nmap -sP -PR 192.168.1.*

configure static(fixed) ip address

Ref to Configure static IP address on Ubuntu 16.04 LTS Server:

Edit the file /etc/network/interfaces:

auto lo
iface lo inet loopback

auto <interface_name>
iface <interface_name> inet static
address <the_static_ip_address_to_be_set>
netmask 255.255.255.0
gateway <gateway>
dns-nameservers <gateway> 8.8.8.8 8.8.4.4

<interface_name> can be found by ifconfig, and <gateway> can be found by route.

Example:

auto lo
iface lo inet loopback

auto enp0s31f6
iface enp0s31f6 inet static
address 10.56.6.90
netmask 255.255.255.0
gateway 10.56.6.254
dns-nameservers 10.56.6.254 8.8.8.8 8.8.4.4

And then reboot.

configure dns

How do I set my DNS when resolv.conf is being overwritten?

sudo vim /etc/resolvconf/resolv.conf.d/head

Add the following:

nameserver 8.8.8.8
nameserver 8.8.4.4

And then update:

sudo resolvconf -u

check port usage(either of one)

lsof -i -n -P
netstat -tulpn #--tcp|-t, --udp|-u, -l, --listening
#-p, --program: Show the PID and name of the program to which each socket belongs.
#-n, --numeric: Show numerical addresses instead of trying to determine symbolic host, port or user names.

check the status of a machine's specific port

測試特定的 TCP 連接埠(port)是否有開啟

nc <ip-address> <port-number> -v

Result:

Connection to <ip-address> <port-number> port [tcp/http] succeeded!

check the process using the port <port-number>

lsof -i:<port-number>
lsof -n -i4TCP:<port-number>

setup ssh server

How to Install and Enable OpenSSH on Ubuntu 20.04

sudo apt install -y openssh-server
sudo systemctl status ssh
sudo ufw allow ssh
sudo ufw enable
sudo ufw status

connect to remote host

ssh <username>@<ip-address>

connect to remote host's <port-number>

ssh <username>@<ip-address> -p<port-number>

send command to remote host and get result locally

ssh -t username@host '<your-command>'

keep ssh connection alive

Keep SSH session alive [closed]

Possible solution to packet_write_wait: Connection to xx.xx.xx.xx port 22: Broken pipe.

Create ~/.ssh/config, fill it with:

Host *
    ServerAliveInterval 240

And then:

chmod 600 ~/.ssh/config

remove host name from known_hosts file(used when you have previously failed to login the host)

Error message:

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@    WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!     @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!
Someone could be eavesdropping on you right now (man-in-the-middle attack)!
It is also possible that a host key has just been changed.
The fingerprint for the ECDSA key sent by the remote host is
SHA256:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.
Please contact your system administrator.
Add correct host key in C:\\Users\\xxx/.ssh/known_hosts to get rid of this message.
Offending ECDSA key in C:\\Users\\xxx/.ssh/known_hosts:8
ECDSA host key for 192.168.1.17 has changed and you have requested strict checking.
Host key verification failed.

Solution:

ssh-keygen -R <ip-address>

ssh with password

How to pass password to scp?

sshpass -p <password> scp -r [email protected]:/some/remote/path /some/local/path

check ssh server status

Ubuntu Linux: Start / Stop / Restart / Reload OpenSSH Server

sudo systemctl status ssh

or

sudo service ssh status

allow ssh server starts on reboot

sudo systemctl enable ssh

check whether a service is enabled

How to list all enabled services from systemctl?

sudo systemctl list-unit-files | grep <service-name>

copy multiple directories from remote host to current directory

scp <username>@<ip-address>:/some/remote/directory/\{a,b,c\} ./

copy files listed in a text file

Linux (mv or cp) specific files from a text list of files?

xargs -a filenames.txt cp -t /destination/directory

move files listed in a text file

Linux (mv or cp) specific files from a text list of files?

xargs -a filenames.txt mv -t /destination/directory

scp and rename the file

scp <username>@<ip-address>:/some/remote/directory/<original-name>.zip <new-name>.zip 

rsync: transfer file between local and remote

rsync -P /local/file <username>@<ip-address>:/some/remote/directory

The -P option shows the progress.

rsync: backup

Copying only JPG from a directory structure to another location Linux

Scheduled folder backup

rsync -av --include='*.<filetype>' --include='*/' --exclude='*' --progress --delete --log-file=/home/<username>/$(date +%Y%m%d-%H%M%S)rsync.log /src/dir /dst/dir/dataBackup-$(date +%Y%m%d-%H%M%S)
date +%Y%m%d-%H%M%S >> /home/<username>/rsync_time.txt # record the backup time

-a: maintain directory structure

-v: verbose

--progress: show progress

--delete: delete files deleted in /src/dir

--log-file: save rsync result to a file

rsync: not chown

How to keep rsync from chown'ing transferred files?

rsync -av --no-o --no-g /src/dir /dst/dir

show package description

apt-cache search <package-name>

show package version details

apt-cache show <package-name>

set(or unset) apt proxy

Configure proxy for APT?

In /etc/apt/apt.conf, add something like:

Acquire::http::proxy "http://child-prc/intel.com:913/";
Acquire::https::proxy "https://child-prc/intel.com:913/";

To unset, just clear /etc/apt/apt.conf.

After changing the file, it will take effect immediately.

set(or unset) system proxy

Set system proxy:

export http_proxy=http://child-prc.intel.com:913/
export https_proxy=https://child-prc.intel.com:913/

Unset system proxy:

unset http_proxy
unset https_proxy

In /etc/environment, comment the following lines:

http_proxy="http://child-prc.intel.com:913/"
https_proxy="https://child-prc.intel.com:913/"

apt-get update

apt-get update

Issue with fetching http://deb.debian.org/debian/dists/jessie-updates/InRelease with docker

In Dockerfile, when using docker monsantoco/min-jessie:latest, if met with :

W: Failed to fetch http://http.debian.net/debian/dists/jessie-updates/InRelease  Unable to find expected entry 'main/binary-amd64/Packages' in Release file (Wrong sources.list entry or malformed file)

E: Some index files failed to download. They have been ignored, or old ones used instead.

This can be solved by adding:

printf "deb http://archive.debian.org/debian/ jessie main\ndeb-src http://archive.debian.org/debian/ jessie main\ndeb http://security.debian.org jessie/updates main\ndeb-src http://security.debian.org jessie/updates main" > /etc/apt/sources.list

in front of apt-get update.

install a package

apt-get install -y <package-name>

uninstall a package

What is the correct way to completely remove an application?

apt-get purge -y <package-name>

or

apt-get --purge autoremove <package-name>

upgrade a package

How to upgrade a single package using apt-get?

apt-get install --only-upgrade <package-name>

download .deb and not install

apt-get install --download-only <package-name>

The .deb file will be put in /var/cache/apt/archives.

install a .deb

How to install a deb file, by dpkg -i or by apt?

dpkg -i /path/to/deb/file
apt install -f

ldconfig

error while rtabmapviz tries to load librtabmap_core.so

ldconfig

This solves: error while loading shared libraries....

list packages installed

ldconfig -p | grep <package-name>

list installed packages by apt

apt list --installed

check the installation path of a package

Determine destination location of apt-get install ?

dpkg -L <package-name>

list all version of a package(centos)

yum list --showduplicates <package-name>

search packages(centos)

yum search <xxx>

ex: yum search java | grep 'java-1.8.0-openjdk-'

check size of directories and files

check total size of a directory, containing all its items(the unit is KB, MB or GB depending on its size)

du -sh <dirname>

check total size of a directory in bytes

How can I see a folder's exact size in bytes?

du -s -B1 <dirname>

check all files' size in current dir

du -h * | less

check the size of items in <dirname> and sort them by their size in descending order

du -h --max-depth=1 <dirname> | sort -hr

or

du -h -d 1 <dirname> | sort -hr

sort: -h for recognizing K,M,G, -r for descending order

check file structure

tree

or

tree </path/to/directory>

To list directory only:

tree -d

check memory size

free    # in KB
free -m # in MB
free -g # in GB

or

cat /proc/meminfo

check file system information

df -h

find out the device where a directory is on

df <dirname>

use du with find

find <dirname> -name <filename> -print0 | du --files0-from=- -hc

This will show all matching files with its size in human-readable format.

create a directory <dir-name>(it will create any missing intermediate pathname components)

it will do nothing if <dir-name> already exists, so we can always use this command without checking if a directory already exists!

mkdir -p <dir-name>

manage user and group

Add a User to a Group (or Second Group) on Linux

Linux Show All Members of a Group Command

show all groups on a system

getent group

Sample output:

root:x:0:
daemon:x:1:
bin:x:2:
sys:x:3:
adm:x:4:syslog,ubt
...

show the groups a user account is assigned to

groups

Sample output:

ubt adm cdrom sudo dip plugdev lpadmin sambashare vboxsf

show all members of a group

grep <groupname> /etc/group

Sample output:

vboxsf:x:999:ubt

add a new group

sudo groupadd <groupname>

add an existing user account to a group

usermod -a -G <groupname> <username>

manage privilege of a file

check the status of a file

stat <filename>

Sample output:

  File: 'faster-rcnn.pytorch/'
  Size: 4096      	Blocks: 8          IO Block: 4096   directory
Device: 802h/2050d	Inode: 120856675   Links: 10
Access: (0755/drwxr-xr-x)  Uid: ( 1001/   user1)   Gid: (    0/    root)
Access: 2019-06-27 16:50:29.845551752 +0800
Modify: 2019-06-27 16:51:01.705798284 +0800
Change: 2019-06-27 16:51:01.705798284 +0800
 Birth: -

show the privilege of a file

How to find owner and group of a directory?

stat -c '%A' <filename>

Sample output:

drwxr-xr-x

show the user and group of a file

stat -c "%U %G" <filename>

Sample output:

user1 root

show the size of a file

stat -c "%s %n" <filename>

Sample output:

1091 README.md

show the privilege, size and last modification time of a file

-l for long listing format, -a for showing hidden files, -h for showing size in human readable format

ls -lah <filename>

Sample output:

total 212K
drwxr-xr-x 10 user1 root  4.0K Jun 27 16:51 .
drwxr-xr-x  9 user1 user1 4.0K Jun 27 16:40 ..
drwxr-xr-x  2 user1 root  4.0K Jun 26 10:37 cfgs
-rwxr-xr-x  1 user1 user1 1.1K Jun 26 11:11 class_names.pkl

The 3rd and 4th columns are the user and group of files respectly.

show modification time of files in current directory

-l for long listing format, -d for only showing directories' names rather than their contents

ls -ld *

ls, sorted by modification time

How can I sort the output of 'ls' by last modified date?

sorted by modification time, descending

ls -lt

sorted by modification time, ascending:

ls -ltr

ls, sorted by size

Linux ls Command Sort Files By Size

ls -Slh # -S for --sort=size

ll, the short name for ls -l

ll <filename>

ls, show one item in separate line

ls -1 <dirname>

ls, show directories starting with <S>(only print their names and don't show their children)

ls -d <S>*/

or

echo <S>*/

ls, recursively

ls -R <dirname>

ls, list absolute path(full path)

How can I generate a list of files with their absolute path in Linux?

For directories (the / after ** is needed in bash to limit it to directories):

ls -d -1 "$PWD/"**/

For files and directories directly under the current directory, whose names contain a .:

ls -d -1 "$PWD/"*.*

For everything:

ls -d -1 "$PWD/"**/*

change privilege of a file

chmod [u,o,g,a][+,-,=][r,w,x] <filename>

open all privilege of <dirname> and all its contents

chmod -R 777 <dirname>

change the owner and group of <dirname> and its children to username and groupname

chown -R username:groupname <dirname/*>

Only change owner:

chown -R username <dirname/*>

This solves the error of scp: /dirname/filename: Permission denied, chmod may also help.

manage processes

monitor CPU, memory usage

top
# specify number of iterations as 1
top -n1

or

ps aux

to show full command in ps aux: Viewing full output of PS command

ps aux | less -+S

check CPU, memory usage, sorted by CPU usage

ps --sort=-pcpu

check how long a process has been running

ps -o etime= -p "<pid>"

check the process name given a pid

ps -p <pid> -o comm=

use pid(can be checked by top) to find out the location of the process

readlink -f /proc/<pid>/exe

kill a process

kill -9 <pid>

find

find, ignore "Permission denied" error

Linux / Unix Find Command Avoid Permission Denied Messages

find . -name "<filename>" 2>/dev/null

find, stop after first match

How to stop the find command after first match?

find . ... -print -quit

find files whose names are str1 or str2

How to combine 2 -name conditions in find?

find . \( -name "str1*" -o -name "str2*" \)

find, return full directory

How can I generate a list of files with their absolute path in Linux?

find "$PWD" -name "<filename>"
find "$(pwd)" -name "<filename>"

find file and show their size

find . -name "<filename>" -type f | xargs du -sh

Note that the double quotes around are important!

find directory and don't show their children

How to find only directories without subdirectories? [duplicate]

find . -type d -links 2 ! -empty

find file and only print their filenames(not including their directory path)

find . -type f -printf "%f\n"

find file and only print their basename(without directory path and extension)

How to get only names from find command without path

find . -name "*.jpg" -exec basename \{} .jpg \;  

find files without an extension

find . -type f ! -name "*.*"

find specific type of files in all subdirectories and then delete

find . -name "*.<file-type>" -type f -delete

or

find . -name "*.<file-type>" | xargs -i rm {}

or

find . -name "*.<file-type>" -exec rm {} \;

find empty directory and then delete

find . -type d -empty -delete

find directory(not need to be empty) and then delete

find . -name "<directory-name>" -type d -exec rm -rv {} \;

or

find . -name "<directory-name>" -type d -exec rm -rv {} +

find directory but exclue "."(the current directory)

find . -mindepth 1 -type d

find directory but exclue "." and force delete

find . -mindepth 1 -type d -exec rm -r "{}" \;

find and move

How to integrate mv command after find command?

find . -name "<filename>" -exec mv -t "<dst_path>" {} +

In Mac, where -t is an illegal option to mv:

find . -name "<filename>" -exec mv {} "<dst_path>" \;

find and copy

Find and copy files

find . -name "<filename>" -exec cp {} "<dst_path>"  \;

find and delete

find . -name "<filename>" -exec rm {} \;

find and remove extension

find exec and strip extension from filenames

find . -name "*.<extension>" -exec sh -c 'mv {} $(basename {} .<extension>)' \;

find in current directory, not recursive, max depth = 1

find . -maxdepth 1 -name "<*word*>"

find in specific depth

find . -maxdepth <depth> -mindepth <depth> -name "<*word*>"

find directory with name length greater than <length> and delete

find . -maxdepth 1 -print| awk -F/ ' length($NF)> <length> ' | xargs rm -rf

find file with <word> in its name in the directory <directory>

find <directory> -name "<*word*>"

find file with <word> in its name but exclude that with <not-word>

find <directory> -name "<*word*>" -not -name "<*not-word*>"

find but exclude certain path

How to exclude a directory in find . command

find / -name "<filename>" -not -path "./<the-path-to-be-excluded>*"

find: case-insensitive match

find / -iname "<filename>"

find: suppress No such file or directory error

find / -name "<filename>" 2>/dev/null

find a file and only return its parent directory

find / -name "<filename>" | xargs -I{} dirname {}

find executable file

find / -executable -type f

A directory is executable if one can cd into it. So one might only want to find executable "file".

find files by their modification time

Find files based on modified date(specifying the exact hour) [duplicate]

find . -newermt "<year1>-<month1>-<day1> 00:00:00" ! -newermt "<year2>-<month2>-<day2> 00:00:00"

find last modified file recursively

How to recursively find the latest modified file in a directory?

find . -type f -printf '%T@ %p\n' | sort -n | tail -1 | cut -f2- -d" "

find, using regular expression

To find files like 1567481018771_res.jpg or 1567481433382_res.jpg.

find . -regex "./[0-9]+_res\.jpg"

To find files like 1567481019363.jpg and 1567481189545.jpg.

find . -regex "./[0-9]+\.jpg"

find in multiple directories with same prefix

find <prefix>* -name "*.jpg"

find by modification time

Find Files By Access, Modification Date / Time Under Linux or UNIX

To find a file modified less than 60 days:

find . -name "<filename>" -mtime -60 -print

list all subdirectories' file count

How to report number of files in all subdirectories?

This will show full subdirectory name:

for f in *; do [ -d ./"$f" ] && find ./"$f" -maxdepth 1 -exec echo \; | wc -l && echo $f; done

Sample output:

      72
$RECYCLE.BIN
      17
8.9
       3
System Volume Information

or

This won't show full subdirectory name, but now subdirectory name and its file count are in the same line:

for f in *; do [ -d ./"$f" ] && find ./"$f" -maxdepth 1 -exec echo \;|wc -l|tr '\n' ' ' && echo $f; done|awk '{print $2"\t"$1}'

Sample output:

$RECYCLE.BIN	72
8.9	17
System	3

find by size

How To Find Files Bigger Or Smaller Than X Size In Linux

find . -type f -size +100M

find and count lines

How to count total number of lines of found files?

This will show line counts for each file and finally total line count:

find . -name <pattern> -print0 | wc -l --files0-from=-

This will only show total line count:

find . -name <pattern> -exec cat {} + | wc -l 

print the content of a file

show line number

cat -n <filename>

use cat to write file

cat > <filename> << EOF
<line1>
<line2>
<line3>
EOF

concat files and generate a new one

cat a.txt b.txt > new.txt

use echo to write file

echo "<text>" > <filename>

use printf to write multiple lines to a file

printf '<line1>\n<line2>' > <filename>

If using echo, \n itself rather than a newline will be written to the file.

grep

grep, only show file names

How can I use grep to show just filenames on Linux? [closed]

grep -l 

search for a word, show line number

grep -n ".*<word>.*" <filename>

search for a specific word from specific type of files in a directory

grep '.*<word>.*' -rnw <directory>/*.<file-type>

or

grep -rnw <directory>/*.<file-type> -e '.*<word>.*'

search for a word, but exclude another

Grep regex NOT containing string

grep ".*<word>.*"  -rnw . | grep -v ".*<word_to_exclude>.*"

escape dot in grep

Using grep to search for a string that has a dot in it

grep '<xxx\.xxx>' -rnw <directory>

search file, but just include some types of file

grep <pattern> --include=\*.{type1, type2} -rnw <directory>

search file, but exclude binary files

How to suppress binary file matching results in grep [closed]

grep <pattern> -Irn <directory>

search file, but exclude some files

# process a binary file as if it did not contain matching data
grep <pattern> --exclude=<file-to-be-excluded> -rnw <directory>

search file, but exclude some directories

grep <pattern> --exclude-dir={<directory-to-be-excluded-1>,<directory-to-be-excluded-2>,<directory-to-be-excluded-3>} -rnw <directory>

find file in current directory and fetch the lowest level's directory name

grep <pattern> -rnw . | while IFS= read -r line ; do
>     echo "$line" | cut -d'/' -f2
> done

find and grep

Linux find and grep command together

find . -name '*fname*' -exec grep -H "text" {} \;
# -H: --with-filename

grep and show multiple surrounding lines

#show <b> lines before and <a> lines after the matched lines
<other-command> | grep <pattern> -B <b> -A <a> 
#show <c> lines before and after the matched lines
<other-command> | grep <pattern> -C <c> 
ex: docker run --help | grep runtime -C 5

grep a word within files with specific file extension

grep -rnw --include=\*.<file-extension> ".*<word>.*"

ex: grep -rnw --include=*.sh ".SPARK." .

grep, case-insensitive

grep -i <pattern> -rnw . 

grep, only list file names

grep <pattern> -rl .

grep, not showing file names

How to find lines containing a string and then printing those specific lines and something else

grep <pattern> -rh .

grep, list file not containing specific string

How do I find files that do not contain a given string pattern?

grep <pattern> -rL .

grep, limit number of files read

grep - limit number of files read

grep <pattern> -r . | head -10

grep the beginning of line

Grep beginning of line

grep <^pattern> -r .

grep, show only a part of matching long lines

How to truncate long matching lines returned by grep or ack

grep ".{0,20}<pattern>.{0,20}" -oE .

egrep, search for pattern A or pattern B

egrep "<pattern-A>|<pattern-B>" <filename>

awk

cut a string and get the Nth part

echo "hello world, it's a good day" | awk '{print$3}'
# it's

get the Nth line

awk 'NR==<line_number>' <filename>

split lines in a file by a delimeter and print their Nth token

How to get the first column of every line from a CSV file?

awk -F" " '{print $1}' <filename>

Get last field using awk substr

split lines by "/" and get the last token

awk -F "/" '{print $NF}' <filename>

get length of lines in a file

awk '{ print length }' <filename>

filter lines by their length

How to remove lines shorter than XY?

<other-command> | awk 'length($0) > <number>'
<other-command> | awk 'length($0) == <number>'
<other-command> | awk 'length($0) < <number>'

exclude Nth column

Exclude multiple columns using AWK

awk '{$1="";print}'

split a file into two according to a ratio

Split: how to split into different percentages?

The following split a file to 90% and 10%.

split -l $[ $(wc -l <filename>|cut -d" " -f1) * 90 / 100 ] <filename>

find unique elements from other command's output

<other-command>| sort | uniq

Note that sort is required.

sort command in unix with numeric sort

If one want to do numeric sort(sort as numbers), then use sort -V.

Sort and count number of occurrence of lines

To also get their counts, use uniq -c.

create a .bak file for <filename>

cp <filename>{,.bak}

copy to another file system

How to move a file without preserving permissions

Following command works when /dst/dir is in CIFS.

sudo cp -f --no-preserve=mode,ownership /src/file /dst/dir

sed

To escape multiple leading spaces, add \ before the first space.

get the Nth line

sed -n <line_number>p <filename>

or

sed '<line_number>!d' <filename>

remove the Nth line

Delete specific line number(s) from a text file using sed?

sed -i -e '<start_line_number,end_line_number>d;<another_line_number>d' <filename>

find and replace string

sed -i 's/<regular expression>/<replacement>/g' <filename>

How to replace a string in multiple files in linux command line

To do this recursively:

!sed -i -e 's/<regular expression>/<replacement>/g' $(find . -name *.<file_extension>)

insert a line after Nth line(it will be N+1-th line)

sed -n -i 'p;<line_number>a <line_to_insert>' <filename>

insert a line before first match

sed -i '/<line_to_match>/i <line_to_insert>' <filename>

insert a line after first match

sed -i '/<line_to_match>/a <line_to_insert>' <filename>

delete the <N>-th line from outside the file

sed '<N> d' <filename>

delete <N> random lines from a file and output to another file

How can I delete multiple random lines from a text file using sed?

shuf -i1-$(wc -l < <input_file>) -n<N> | sed 's/$/d/' | sed -f- <input_file> > <output_file>

remove part of a line

sed -i 's/<word_to_delete>//' <filename>

remove lines starting with a specific word

How to delete a line with a given word using SED function

sed -i '/^<word>\b/d' <filename>

Input file:

a
a b
b a
ab
ad
b

Result file:

b a
ab
ad
b

add prefix or suffix to all lines of a file

Add a prefix string to beginning of each line

This will edit the file in-place:

sed -i -e "s/^/<prefix>/" <filename>
sed -i -e "s/$/<suffix>/" <filename>

To create a new file:

sed -e "s/^/<prefix>/" <filename> > <newfilename>
sed -e "s/$/<suffix>/" <filename> > <newfilename>

Example: add current directory as prefix to a file How do I replace a token with the result of pwd in sed?

sed -i -e "s?^?`pwd`/?" <filename>

sed, reserve windows line ending

sed -bi '/View.*\(.*\);/d' <filename>

-b for binary mode, which prevents from changing windows line ending to that of linux. -i for inplace.

sed, search for lines with certain pattern

sed

Print such lines:

sed -n '/ <pattern>/p' "<infile>"

Write such lines into another file:

sed -n '/ <pattern>/w <outfile>' "<infile>"

translate or delete characters - squeeze multiple <char> into one

tr -s '<char>'

delete characters

tr -d '<char>' 

To use it with a file:

tr -d '<char>' < a.txt > tmp.txt && mv tmp.txt a.txt

replace characters

How to concatenate multiple lines of output to one line?

tr '\r' ';'

This replace the Windows ^r with ;.

delete '\r' in files

What is ^M and how do I get rid of it?

dos2unix <filename> #support the wildcard sign *

Or in vim:

:%s/^M/\r/g

cut, remove first N characters

What is a unix command for deleting the first N characters of a line?

cut -c 3-

cut, remove last character

Remove last character from line

This only works when all lines' ending characters are the same.

# -107.484, 56.0374, -3.34328)
# -79.6017, 21.7058, -1.92846)
cut -d "%" -f1
# -107.484, 56.0374, -3.34328
# -79.6017, 21.7058, -1.92846

cut by <delimiter> and get the specific <fields>

cut -d '<delimiter>' -f <fields>

<fileds> could be 1 or "1,2,3".

cut a file by <delimiter> and get the specific <fields>

cut -d '<delimiter>' -f <fields> <filename>

cut by space and get the last element

<other-command> | rev | cut -d' ' -f1 | rev

cut by space and remove the last element

How to exclude last N columns in Bash on Linux?

<other-command> | rev | cut -d ' ' -f2- | rev

join by a delimiter

How to join multiple lines of file names into one with custom delimiter?

<other-command> | paste -sd "," -

-s for serial, -d "," for delimiter

remove a specific character

remove particular characters from a variable using bash

<other-command> | tr -d '.'

reverse the output of <other-command>

<other-command> | tac

reverse a file

tac <filename> > <output_filename>

use curl to test REST API

使用curl指令測試REST服務

curl -X POST "<ip-address>:<port>/<subpage>" -d "<param1>=<value1>"

download a file as </directory/file_name> from a url

Method 1: wget

wget -O </directory/file_name> <your-url>

Method 2: curl

curl -o </directory/file_name> <your-url>

curl, ignore SSL certificate warnings

How to ignore invalid and self signed ssl connection errors with curl

curl https://<ip-address>:<port>/<subpage> --insecure

download a file to <dirname> from a url

wget -P <dirname> <your-url>

download a file only when the file does not exist

wget -nc <your-url>

iterate over the result of some command

<some-command> | (while read x; do <other-command-using-$x>; done)
ex: ls *.json | (while read x; do echo $x; done)

let the command 'history' show the time when the commands are executed

export HISTTIMEFORMAT='%F %T '
history

output non-ascii characters in shell

method 1:

echo -en "$(<some-command-printing-non-ascii-characters>)"

method 2:

How to convert \uXXXX unicode to UTF-8 using console tools in *nix

apt-get install uni2ascii
<some-command-printing-non-ascii-characters> | ascii2uni -a U -q

write or append the output to a file

<some-command> > <filename> #overwrite the file
<some-command> >> <filename> #append to the file

redirect the error message of a command to a file

Shell重定向 &>file、2>&1、1>&2 、/dev/null的区别

<some-command> > <filename.txt> 2>&1

0, 1, 2 here means standard input, standard output and standard error respectively.

> <filename.txt> means redirect standard output to the file.

2>&1 means redirect standard error to standard output.

redirect to both file and console(stdout)

How to redirect output to a file and stdout

apt-get install expect #install unbuffer
unbuffer <some-command> 2>&1 | tee <filename.txt>

count number of lines of other command's output

<some-command> | wc -l

zip <foldername> to <foldername>.zip

zip -r <foldername>.zip <foldername>

unzip troubleshooting: please check that you have transferred or created the zipfile in the appropriate BINARY mode and that you have compiled UnZip properly

Cannot open large concatenated zip file with unzip, though it opened fine with Archive Utility, get a central directory error

zip -F <name>.zip -O <name>.fixed.zip
zip -FF <name>.zip -O <name>.fixed.zip

unzip a file to a desinated directory

unzip <xxx.zip> -d </target/directory/to/unzip>

pack to a tar file

tar cf </packed/file/name>.tar </folder/name>

compress to a gz file

gzip -c </src/file/name> > </zipped/file/name>.gz

compress to a tar.gz file

tar -czvf <xxx.tar.gz> <xxx>

compress to a tar.gz file, excluding some subfolders

tar -czf <parent.tar.gz> <parent> --exclude <parent>/<child1> --exclude <parent>/<child2>

show progress bar while compressing to a tar.gz file

Is there a way to see any tar progress per file?

tar cf - <xxx> -P | pv -s $(du -sb <xxx> | awk '{print $1}') | gzip > <xxx>.tar.gz

Mac OS:

tar cf - <xxx> -P | pv -s $(($(du -sk <xxx> | awk '{print $1}') * 1024)) | gzip > <xxx>.tar.gz

show progress bar while unzip a tar.gz file

pv <xxx.tar.gz> | tar xzf - -C </target/directory/to/unzip>

extract .tar file

tar -xvf <xxx.tar>

extract .tar.gz file

tar -xzvf <xxx.tar.gz>

extract .tar.gz file to a directory

tar -xzf <xxx.tar.gz> -C <xxx>

extract .gz file

gunzip <xxx.gz>

or

gzip -d <xxx.gz>

extract .7z file

7za -y x <xxx.7z>

extract .bz2 file

How to decompress a .bz2 file

bzip2 -dk <xxx.bz2>

extract .rar file

How to Extract RAR Files in Ubuntu Linux

How to Open, Extract and Create RAR Files in Linux

extract .rar file, keep its structure:

unrar x <xxx.rar>

extract all its content to the same place:

unrar e <xxx.rar>

Unrar all file in directory without prompting

To disable the prompt, add -o+ option for automatic overriding.

List files in rar:

rar l <xxx.rar>

go to the previous directory

cd -

check directory stack

dirs

operate with directory stack

Change directory and push it to directory stack:

pushd <dir>

Pop a directory from the stack and cd to it:

popd

show difference between two files

diff <file1> <file2>

To ignore changes in spaces:

How can I diff 2 files while ignoring leading white space

diff <file1> <file2> -b

show difference between two directories

Given two directory trees, how can I find out which files differ?

diff -rq <dir1> <dir2>

-r for recursive, -q for short mode.

show difference between two directories, exclude some files

diff -x "<pattern>" -r -q <dir1> <dir2>

compare the two files

cmp <file1> <file2>

Sample output:

<file1> <file2> differ: byte 132161537, line 437952

compare the two files, show the different byte position and their values

cmp -l <file1> <file2>

Sample output:

132161537 274   0
132161538   4   0
132161539 225   0
132161540 212   0
132161541 261   0
...

compare the two so or executable files

compare executable or object file

diff <(objdump -d <file1>) <(objdump -d <file2>)

list the last <num_lines> lines in <filename.txt>

tail -n <num_lines> <filename.txt>

Display the output of <other-command> in pages and scroll to bottom automatically

+G performs the action of scrolling to bottom automatically

<other-command> | less +G

run your command in background, write its output to a text file, and watch that text file simultaneously

nohup <some-command>  </dev/null &><filename.txt> &
tail -f <filename.txt>

create a symbolic link(soft link) of <target> named <linkname>

ln -s <target> <linkname>

The <target> should be full path, otherwise there will be an error: Too many levels of symbolic links.

Difference of hard and soft link can be found in: Linux ln command.

To check if it successes, use:

ls -l <linkname>

If it successes, it will output something like:

lrwxrwxrwx 1 root root 50  四  29 10:21 Eigen -> /home/ubt/Documents/installation/eigen-3.3.4/Eigen

in blue.

Otherwise it will output something like:

total 0
lrwxrwxrwx 1 root root 41  四  29 10:18 Eigen -> /Documents/installation/eigen-3.3.4/Eigen

in red.

check the target of a symbolic link

readlink <linkname>

remove a symbolic link

Reference to: How to Remove a Symbolic Link (Symlink)

unlink <linkname>

or

rm -rf <linkname>

mount /src/dir to /dst/dir

mkdir <dir2>
mount --bind /src/dir /dst/dir

--bind makes it possible to remount part of the file hierarchy somewhere else, and it solves the error:

mount: /src/dir is not a block devicere

In VirtualBox's VM, assume /media/sf_implementation is a directory shared from host. Use the following to create a binding from /home/ubt/Documents/implementation to /media/sf_implementation:

mkdir /home/ubt/Documents/implementation
cd /media
sudo mount -t vboxsf implementation /home/ubt/Documents/implementation

mount CIFS

mount -t cifs -o vers=1.0,user=<cifs_user_name>,password=<cifs_user_password> "//<ip_addr>/src/dir" /dst/dir

To specify the owner of the mount point, ref: How to chown a file on a CIFS mount?:

-o uid=<linux_user_name>

unmount /dst/dir

umount /dst/dir

NTFS partition is read only, to make it writable

Why does my NTFS partition mount as read only?

sudo ntfsfix /dev/sdb1

And then umount and mount it again.

get the full path of a symbolic link's target

readlink -e <linkname>

readlink -f returns something even for non-existing <linkname>

readlink -e will return null for non-existing <linkname>

check the contents of jar file

jar tf <xxx.jar>

or

jar tvf <xxx.jar>

check the contents of a zip file

less <xxx.zip>

perform set operation on <file1> and <file2>

find the difference set of <file1> - <file2>

comm -23 <(sort <file1>) <(sort <file2>)

find the intersection of <file1> - <file2>

comm -12 <(sort <file1>) <(sort <file2>)

Note that '<' and '(' should not be separated, otherwise there will be a bash: syntax error near unexpected token `('!

More details: Linux comm command brief tutorial

bash, Linux: Set difference between two text files

ab: test the capability of serving of a website

ab -n <total-requests-count> -c <concurrent-requests-count> <url>

send <total-requests-count> requests to <url> in which of them are concurrent.

to exit X server

sudo service lightdm stop

ls and then copy

pass output as an argument for cp in bash [duplicate]

cp `ls /source/directory` /target/directory

ls and then delete

rm `ls /dirname/filename`

copy: force copy to overwrite without confirmation

How to force cp to overwrite without confirmation

yes | cp -rf xxx yyy

copy : Argument list too long

Argument list too long when copying files

for i in *.jpg; do cp "$i" /target/directory; done

move only files with specific type from a directory structure to another directory

find /src/dir -type f -name "*.<filetype>" -exec mv {} /dst/dir \;

ls: Argument list too long

What is the fastest way in terminal to get a count of files when there is so many of them? [duplicate]

Use the following to replace ls /dir/name/*.jpg | wc -l

ls -l /dir/name | fgrep .jpg | wc -l

alias: list alias set previously

alias

alias: create a alias of a command

alias <dst_excutable>=<src_executable>

e.g.

alias python=python3

rename a batch of files

how can I rename multiple files by inserting a character?

Rename file with extension .cloud to _cloud.cpp.

rename 's/.cloud$/_cloud.cpp/' *.cloud

Rename: delete something like -0_1, -1_2 from file names:

rename 's/-[0-9]_[0-9]//' *

Rename: add and remove .bak:

rename 's/$/.bak/' *
rename 's/.bak//' *

Or using for loop and mv:

for f in $(ls *.xml); do
    mv "$f" "cam2_$f"
done

This replace ./FP32/b4/benchmark_CPU__.txt to ./FP32/b4/benchmark_CPU_t1_s1.txt:

for fname in $(grep FPS -rl . --include=*__.txt)
do
    mv "$fname" "${fname/__/_t1_s1}"
done

how to rename multiple files by replacing string in file name? this string contains a "#"

This replace *abc*recon.pcd to *def*recon.pcd:

find . -type f -name '*recon.pcd' | while read FILE ; do
    newfile="$(echo ${FILE} |sed -e 's/abc/def/')" ;
    mv "${FILE}" "${newfile}" ;
done

combine files by column, and set the column width

A better paste command

paste <filename1> <filename2> ... <filenameN> | expand -t <column_width>

The above requires one to find <column_width> manually. The following find the suitable <column_width> automatically.

paste <filename1> <filename2> ... <filenameN> | expand -t $(( $(wc -L < <filename1>) + 2 ))

shuffle a file

shuf <filename>

random sample from a file

How to randomly sample a subset of a file

Sample <count> lines from <filename>.

shuf -n <count> <filename>

random sample from a file(in-place)

shuf -n <count> <filename> -o <filename>

to make core dumped into current directory

Where do I find the core dump in ubuntu 16.04LTS?

Core dumped, but core file is not in the current directory?

ulimit -c unlimited
echo "core.%e.%p" > /proc/sys/kernel/core_pattern 
sudo service apport start

Note: if you are inside a docker container, do the above on the HOST!

use gdb to debug core file

How do I analyze a program's core dump file with GDB when it has command-line parameters?

gdb <executable> <core_file>

or

gdb <executable> -c <core_file>

open a file in read-only mode

view <filename>

check if a directory exists

Check if a directory exists in a shell script

if [ -d "$DIRECTORY" ]; then
  # Control will enter here if $DIRECTORY exists.
fi
if [ ! -d "$DIRECTORY" ]; then
  # Control will enter here if $DIRECTORY doesn't exist.
fi

for loop in bash

Bash For Loop Examples

for OUTPUT in $(<other-command>)
do
   command1 on $OUTPUT
   command2 on $OUTPUT
   commandN
done

continue

Bash: Continue In a For / While Loop

for i in 1 2 3 4 5 6
do
    ### just skip printing $i; if it is 3 or 6  ###
    if [ $i -eq 3 -o $i -eq 6 ]
    then
        continue  ### resumes iteration of an enclosing for loop ###
    fi
    # print $i
    echo "$i"
done

pass argument to shell script

How can I pass a command line argument into a shell script?

In your script, use "$1", "$2", ... to represent the first, second, ... arguments.

Propagate all arguments in a bash shell script

To pass all arguments at one time, use "$@", for example, writing a script file named compile.sh with following content:

g++ "$@" -I /usr/local/include/opencv4 -L /usr/local/lib -lopencv_imgcodecs -lopencv_core -lopencv_imgproc -lopencv_highgui -std=c++11

And by using:

./compile.sh a.cpp b.cpp c.cpp

All a.cpp, b.cpp, c.cpp will be the argument of g++.

get video codec

how to recognize video codec of a file with ffmpeg

mediainfo --Inform="Video;%Codec%" <input_video>
# AVC
ffprobe -v error -select_streams v:0 -show_entries stream=codec_name -of default=noprint_wrappers=1:nokey=1 <input_video>
# h264

get video information

How to retrieve video file information from command line under Linux?

ffprobe -v error -show_format -show_streams <input_video>

video convertion: mp4 to h264

Converting .mp4 to .264 using FFmpeg

ffmpeg -i <input_mp4_video> -an -vcodec libx264 -crf 23 <output_h264_video>

nano editor

First edit your file:

nano xxx.txt

Then press Ctrl + O to save, press Enter to confirm, and finally press Ctrl + X to exit.

emacs editor

Emacs Basics Save: Ctrl + x Ctrl + s

Quit: Ctrl + x Ctrl + c

P.S. This also works for Mac, no need to replace Ctrl with Command.

open folder from terminal

How to open a folder in linux via terminal? [closed]

nautilus <folder_name>

open file from terminal

How to open text, video, or picture files via terminal

xdg-open <filename>

restart gnome

How to restart GNOME Shell from command line?

killall -SIGQUIT gnome-shell

find default video player's name

Ubuntu (GNOME) default video player name

Find in launcher configuration file:

grep -r Videos /usr/share/applications

Sample output:

/usr/share/applications/org.gnome.Totem.desktop:Name=Videos
/usr/share/applications/totem.desktop:Name=Videos

Find out which package supplies this file:

dpkg -S /usr/share/applications/org.gnome.Totem.desktop
dpkg -S /usr/share/applications/totem.desktop

Sample output:

totem: /usr/share/applications/org.gnome.Totem.desktop
totem: /usr/share/applications/totem.desktop

So the default video player is totem.

open pdf viewer from terminal

evince xxx.pdf

record gif

How to create animated GIF images of a screencast?

sudo apt install -y peek
peek

run cron job

crontab -e

Add something like:

0 3 * * * clamscan -r / > $(date "+%Y-%m-%d-%H-%M-%S".txt)

How to run a cron job using the sudo command

Run with sudo:

sudo crontab -e

Ubuntu cannot boot: platform MSFT0101:00 failed to claim resource 1, acpi MSFT0101:00: platform device creation failed: -16

Failed to claim resource 1 boot error

Failed to claim resource error

On Windows-Ubuntu dual boot, if you cannot boot into ubuntu, showing black screen with these errors, try disable TPM from BIOS.

Ubuntu cannot boot: black screen with blinking underscore

Ubuntu open to black screen with blinking underscore character after release upgrade

sudo dpkg-reconfigure gdm3
sudo service gdm3 restart

ubuntu kde

solve tags.so high cpu usage

[SOLVED] Baloo using 30% of CPU and Kded5 10% python 11% total 61% at idle

balooctl status
balooctl disable
rm -rf ~/.local/share/baloo

Also: System Settings -> Search -> File Search -> Uncheck Enable File Search

About

Some useful linux, git, vim and docker commands

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages