ip l show
ip a show eth0
route -n
ip r
ip r | grep default
ip l show | grep -A1 eth0
ip l show | egrep -A1 'eth0|lo'
sudo ifconfig
sudo /sbin/ifconfig
nmcli device
nmcli connection
nmcli radio
nmcli wifi off
nmcli wifi on
nmcli device wifi list
nmcli device wifi connect nixcraft_guest passowrd {my password here}
nmcli wwan off
nmcli wwan on
for i in*.png;do mv"$i""${i%.*}_3.6.14.png";done# It replaces .png in all the .png files with _3.6.14.png.mv$i${i%.*}_3.6.14.png Rename original .png files with the filename+_3.6.14.png. # ${i%.*} Anything after last dot would be cutdown. So .png part would be cutoff from the filename.
cloning usb stick to an image
download etcher from download and install linux x64 version
you can do this when you have either img file or bootable diskinternals
I want to create a symlink for all hidden files excluding the hidden
directories. I want to put my configuration files in my home directory
into version control.
I need to exclude the hidden directories because they contain binaries
and what not.
Using this one liner, I can create a symbolic link for every hidden file
in my home directory to my working (or target) directory.
for f in .*;do if[[-f$f]];then ln-s /home/me/$f /home/me/working/directory/$f;fi;done
I execute this from my source directory.
I can verify that my symlinks exist in my target directory
cd /home/me/working/directory
ls-lah
I should see a few lines that look like this:
lrwxrwxrwx. 1 me me 26 Jan 27 10:49 .zsh_history -> /home/me/.zsh_history
lrwxrwxrwx. 1 me me 20 Jan 27 10:49 .zshrc -> /home/me/.zshrc
This post forms part of a sequence of command line references that I
will be writing where I forget the command or its syntax or find it
interesting enough to document it.
Although it is easily Google-able, there are usually a chain of
commands that I want kept together for ease of use.
The commands I use should be universal but just in case, I am
running Fedora release 28 (Twenty Eight) and Zsh.
As far as I understand, bash±tar files typically have the tar or tar.gz
extensions. bash±tar is the archive that can preserve permissions and directory
structures and tar.gz is the compressed bash±gzip-ed bash±tar file.
Listing the contents of an archive
You can list the contents of an archive bash±tar -tf archive.tar.gz
without having to extract the data. When you want to see more information
bash±tar -tvf archive.tar.gz prints out details about the file including the
permissions, owner, group, file size, modified date and time and the filename.
Your can also run these commands as bash±tar --file archive.tar.gz --list and
bash±tar --list --file archive.tar.gz --verbose
What are all these switches?
**t
– list**: tells tar to list everything in the archive
**v
– verbose**: verbose (optional), prints out as much information as it can for you
f | – file: specifies the file of the archive you want to list.
(This must always be the last flag
as it precedes the filename in the command unless you use –file)
Creating a new archive
Archive
Create a new archive for the awesome directory
bash±tar -cf archive.tar awesome or bash±tar --create --file awesome. You can create
an archive from any working directory bash±tar -cf archive.tar /home/user/workspace/awesome
bash±tar -cvf archive.tar awesome or bash±tar --create --verbose --file awesome will print
out the files names that have been added to the archive.
Compressed archive
The same switches apply as above, however we include the -z or --gzip switches to indicate
that the archive must be compressed.
Create a compressed gziptar archive bash±tar -cvzf archive.tar.gz awesome.json
or bash±tar --create --verbose --gzip --file archive.tar.gz awesome.json
for the awesome.json file.
Create a compressed gziptar archive bash±tar -cvzf archive.tar.gz /path/to/awesome
for the /path/to/awesome directory.
If you want to archive and compress multiple members, chain the files and directories
bash±tar -cvzf archive.tar.gz /path/to/something another/awesome somewhere/sauce.json
Exclusions
In cases where you need to omit specific files or directories from an archive
bash±tar -czvf archive.tar.gz --exclude=\*.gz /path/to/awesome will exclude
any files with the .gz extension when creating the archive on path /path/to/awesome.
To exclude multiple files and directions, exclusions can be chained
bash±tar -czvf archive.tar.gz --exclude=something.tar --exclude=somewhere /path/to/awesome
The archive on path /path/to/awesome is created which excludes the something.tar
file and somewhere directory.
What are all these switches?
**c
–create**: tells tar to create a new archive
**v
–verbose**: verbose (optional), prints out as much information as it can for you
**z
–gzip**: compresses the archive using gzip _(you can also use
-j
–bzip2 for bzip2 compression)_
f | –file: specifies the file of the archive name that you want to create.
(This must always be the last flag as it precedes the file name in the command unless
you use the –file switch)
–exclude: exclude files given as a pattern
Clean up
Sometimes I need to clean up files in a directory once I have created the archive. An example
is one of my logs directories. Once I have archived all the logs, I want to delete
them but keep my archive and any existing archives. Using Zsh I run
# In Zsh, you can use ^ to negate# a pattern with extendedglob enabled
setopt extendedglob
rm-- ^*.tar.gz
Updating an archive
bash±tar cannot update compressed archives so you need to create an uncompressed archive.
The -u or --update switch will append files newer than the copy in the archive.
# Create a new archive without compressiontar-cvf archive.tar /path/to/archive
# Update or add files in /path/to/archive# tar will update and changed or added filestar-uvf archive.tar /path/to/awesome
# You can then compress your archivegzip archive.tar.gz
Extracting data
Extract data bash±tar -xf archive.tar or bash±tar --extract --file archive.tar
to its local directory.
Extract a compressed archive
bash±tar -xvzf archive.tar.gz to its local directory. This extracts the files and
decompresses them using gzip.
Extract a compressed archive to another directory
bash±tar -xvzC /path/to/awesome -f sauce.tar The files in sauce.tar.gz are extracted to
/path/to/awesome
What are all these switches?
**x
extract**: tells tar to extract the files from the archive
**v
–verbose**: verbose (optional), prints out as much information as it can for you
**z
–gzip**: tells tar to decompress the archive using gzip - you can use **j
–bzip2** for bzip2
**C
–directory**: change to directory
f | –file: specifies the file of the archive you want to extract. (This must always be the last flag
as it precedes the filename in the command)
Extracting specific files
You don’t have to extract the entire file. If you have a tar file but are only interested in
a few files then grab them out of the tar file:
List the contents of the tar file bash±tar -ztvf archive.tar.gz | grep filename and
look for the file name(s) you are interested in.
Extract the file bash±tar -zxvf archive.tar.gz ./awesome.clj or directory
bash±tar --extract --file=archive.tar.gz src
Extracting wildcards
If you can’t use a rigid file name or directory then you can use wildcards (globbing patterns)
bash±tar -xf archive.tar --wildcards --no-anchored "*.clj"
--wildcards tells tar to accept globbing patterns while --no-anchored tells it that
the pattern applies to the member names after any / delimiter.
Comparing against the file system
-d, --diff, --compare
finds the differences between the archive and the file system.
The arguments are optional and you specify archive members to
compare. If not given, the current working directory is assumed.
This is handy when you want to see whether there are files in the
bash±tar file that are not yet in the local filesystem or visa versa.
It also reports differences in attributes including the file size,
mode, owner, modification date and contents.
The following example compares the archive members ‘rock’, ‘blues’
and ‘funk’ in the archive ‘bluesrock.tar’ with files of the same
name in the file system. (Note that there is no file, `funk’; tar
will report an error message.)
# this section and example was found at gnu.org# http://www.gnu.org/software/tar/manual/html_node/compare.htmltar--compare--file=bluesrock.tar rock blues funk
rock
blues
tar: funk not found in archive
Using zip
This is outside the scope of this post but I have the base commands available for in
case I need them.
To list the contents of the archive without unzipping it
bash±less file.zip or bash±unzip -l file.zip.
To create a new archive bash±zip file.zip file1 file2 file3 or for
a directory bash±zip -r file.zip /path/to/archive.
To exclude files from an archive being created
bash±zip -9 -r --exclude='*.zip' file.zip /path/to/archive.
less archive.zip
unzip -l archive.zip
zip archive.zip file1 file2 file3
zip -r archive.zip /path/to/archive
zip -9-r--exclude='*.zip' archive.zip /path/to/archive
unzip archive.zip
For a long list: Enter to continue to read, q to quit.
Ctrl + A : move cursor to the beginning of the line.
Multiple commands
# run at once
command_1 && command_2
.sh file
# using script: file.sh#!/bin/shecho'some info'
command_1
command_2
# and then sh file.sh
# with arguments$file1=$1wc$file1# word count# multiple input argsfor FILE1 in"$@";do
wc$FILE1done
NAME="defaut"# default value! DON'T HAVE SPACE!!!# with flagswhile getopts n:f: option;do
case"${option}"in
n)NAME=${OPTARG};;
f)FILE=${OPTARG};;esacdone
echo$NAMEwc$FILE# how to use?
sh test.sh -n"ThiD"-f test.md
Search / grep / sed
# all files / folders containing 'abc'ls | grep-i abc
# search and extract a part of result
pip show numpy
# Location: /usr/lib/python3/dist-packages
pip show numpy | sed-n's/Location: //p'# /usr/lib/python3/dist-packages
Check info
System
# Change password
passwd
# DISK SPACEdf-h
# like monitor
top
# MEM USAGE
free -m
# ALL ENVprintenv# add newexport ABC=/xyz/thi/
# NVIDIA
nvidia-smi
lspci -nn | grep'\[03'# another way
I have copied a test.txt file with a frontmatter into the first line of a file.
sed-n'/aa/,/fff/p' file1 > tmp
sed'1 r tmp' file2
# CHANGE ACTIVE DIRcd <dir>cd# to the startup dircd / # to rootcd .. # to father dircd - # back to previous dir
# CREATE NEW FOLDERmkdir <dir>
# LISTls
ls-a# including hiddenls | grep'ubuntu'# files containing 'ubuntu' in name
# CURRENT PATHpwd
# FOLDER/FILE SIZEdu-hs <directory / file>
# `h` : human readable (6.7G)# `s` : display size# all folders/files of current folderdu-hs* | sort-rh# only foldersdu-sh ./*/
# only first 5 retrievesdu-h /home/thi/ | sort-rh | head-5
# REMOVINGrm <file>
rm-f <file> # force to removerm-rf <dir># remove folderrmdir <empty-dir> # remove empty
# COMPRESS
zip file.zip file/folder
unzip file.zip # decompress
# PRINT TREE folder structure
tree
tree -d# only folders
tree -d-I'abc'# except folder "abc"
tree -I'abc|xyz'# except folder "abc" and "xyz"
tree -I'test_*|__pycache__|__init__.py'# use wildcat
tree -L 2 # level 2
tree -P'test_'# list only files starting with "test_"
# (1st test on mac)# Look for which ports are running# Find their PID# Kill them
brew install nmap
nmap localhost
lsof -i:8080
kill <PID>
# in case above command cannot solve the problemsudo lsof -i-P | grep"8080"
Text file
# QUICK LOOK CONTENT
more file.txt
cat file.txt
# JUST CREATEtouch file.txt
# CREATE + MODIFY
nano file.txt # Ctrl+X to quit
vim file.txt # ESC, :q to quit
# SEARCH STRINGgrep"string" file.txt
# ADD A LINE TO A FILE WITHOUT OPENNING ITecho"hello 'thi' world">> my_file.txt
# CREATEalias yourAlias='cd /usr/'alias yourAlias=cd /usr/ # windows
# CALL
yourAlias
# LIST OF ALIASESalias
alias abc # "abs" stands for what?
# remove an aliasunalias abc
# group of commands
my_alias(){
screen -S dat -dm bash -c"cd /dinhanhthi.com; iserve; exec sh"}
# list of commands
my_alias(){cd /home/user/git/abc/
git add .
git commit -m"abc"
git push
}
Linux / MacOS: Add your alias to .bash_aliases (in home dir, printenv HOME) if you wanna store your alias permanently.
Windows: Using cmder (its setting file), add more aliases to <cmder-install>/config/user_aliases.cmd. You can also add (automatically) on the cmder UI, it adds them for you to the .cmd file.
Create / Copy / Cut / Paste
# Create a new foldermkdir <folder>
mkdir-p <folder> # already exist accepted
# MOVEmv <old-dir> <new-dir>
move <old-dir> <new-dir> # windows
# RENAME a file/foldermv olname.txt newname.txt
# COPYcp file file
cp-r file&dir file&dir
Display
# only display 3 last directory namesPROMPT_DIRTRIM=3
Create key with different names, e.g. id_rsa.home, id_rsa.work.
Add to ~/.ssh/config,
Host home
Hostname home.example.com
IdentityFile ~/.ssh/id_rsa.home
User <your home acct>
#
Host work
Hostname work.example.com
IdentityFile ~/.ssh/id_rsa.work
User <your work acct>
Add to ssh-agent (don’t need to retype password again)
# copy with sudo on remote# 1. copy to a place you have permissions
scp * thi@remote:/home/thi/tmp/
# 2. move to the place you want
ssh thi@remote sudo mv /home/thi/tmp/\* /place/we/want
# exit background runningsudo apt install net-tools
netstat -lepunt# kill a process, e.g. 29231/sshkill <pid> # eg. kill 29231
- `-C`: use data compression.
- `-f`: Requests ssh to go to background just before command execution
- `-L`: local port forwarding[link](https://help.ubuntu.com/community/SSH/OpenSSH/PortForwarding).
- `-N`: Do not execute a remote command. This is useful for just forwarding ports
- `-p <port>`: port to connect.
- `-q`: quiet mode.
- `-v`: verbose mode.
- `-X`: running GUI remote app locally.
Errors
- REMOTE HOST IDENTIFICATION HAS CHANGED
@@ Offending ECDSA key in /home/thi/.ssh/known_hosts:21
+ SOLUTION:
! Open /home/thi/.ssh/known_host and delete line 21
The following wiki, pages and posts are tagged with