bash - useful commands
Great general resource for shell scripting
Run bash/sh script (alternatives)
bash my_script.shor
source my_script.shor
./my_script.shDon’t forget: Script has to be executable, e.g. via
chmod +x my_script.shcreate several files at once
touch {1,2,3,4}.mdNavigate
Create directory and cd to it right away
mkdir foo && cd $_$: Contents of a variable (more information can be found here)
_: Last argument of previous command (more information)
Write
Redirect stdout & stderr
Example:
grep -i 'abc' content 2>/dev/null
// or
npm list -g --depth=0 2>/dev/nullSyntax:
1> fileredirects stdout to file> fileredirects stdout to file (shorthand for1>)2> fileredirects stderr to file&> fileredirects stdout and stderr to file
/dev/null is the null device. It takes any input you want and throws it away. It can be used to suppress any output.
Redirect stderr to stdout before piping
app 2>&1 | grep helloPoints file descriptor 2 to where file descriptor 1 is already pointing.
Here the file descriptor to redirect (i.e. 2) gets redirected to the already set up file descriptor 1.
Run script, but show stderr in stdout:
#!/bin/sh
exec 2>&1
exec node server/run.js#!/bin/bash
file=/wait/done
>&2 echo "Sleeping for 10 s."
sleep 10
touch $file
>&2 echo "Created file $file."Search / Find
Shell
-
Find something in directory tree.
find /root/directory/to/search -name 'filename.*' -
Find some process/file which runs for an app
E.g. for
mysqlthere is amy.cnffile. To find which one is in use runsudo fs_usage | grep my.cnffs_usageshows running programs live
VIM
```vim
:e **/filename.cpp
```User generation
Give group ownership of Laravel directory structure to the web group www-data
sudo chown -R :www-data /var/www/laravelCommands
Find (file or directory names)
-
e.g. set
chmodfor folders and separately for filesTo change all the directories to 755 (-rwxr-xr-x):
find /opt/lampp/htdocs -type d -exec chmod 755 {} \;To change all the files to 644 (-rw-r—r—):
find /opt/lampp/htdocs -type f -exec chmod 644 {} \;
Permissions, chmod
The following two commands are equivalent
```bash
chmod u=rwx,g=rx,o=r myfile
chmod 754 myfile
```- The letters u, g, and o stand for ”user”, ”group”, and ”other”
- 4 stands for “read”,
- 2 stands for “write”,
- 1 stands for “execute”, and
- 0 stands for “no permission.”
Combinations
Automate git checkout — my_file
git status | grep modified | sed 's/^.*modified: //' | xargs git checkoutxargs: —> UNDER CONSTRUCTION!
Move files/folders with rsync
Alternative to mv backup/ backupArchives/
rsync -a backup/ backupArchives/
rm -rf backup/*Internet
download files
Downloads file to current folder from web
wget http://files.fast.ai/files/setup_p2.shvisit websites
curl http://somewebsite.comCount
Number of files in folder train/dogs:
ls -l train/dogs/ | wc -lPipeline continuation / Process expansion
Taken from this SO answer:
Arguments and are made available to
rmthrough a special variable (called argv internally). The standard input, on the other hand, looks to a Unix program like a file named stdin. A program can read data from this “file” just as it would if it opened a regular file on disk and read from that.
Use xargs to circumvent this.
find . -name ".txt" | grep "foo" | xargs rmvia `…` or $(...)
e.g. docker rmi $(docker images | grep "^<none>" | awk '{ print $3 }')
$#, $?, $@ etc.
"$@" stores all the arguments that were entered on the command line, individually quoted ("$1" "$2" …).
For example for the following command
./command -yes -no /home/username$@would give you the array{"-yes", "-no", "/home/username"}- And
$0= ./command,$1= -yes etc.
Background jobs (new threads / processes)
-
Append
&to run command/script as background jobsleep 5 && ls | grep "foo" & -
View all currently running background jobs (
-padd more information)jobs -p -
Kill job
kill %1 -
Kill application
Example: To kill
Dockerapplication on Mac OS X:killall DockerRestart
DockerApplication:killall Docker && open /Applications/Docker.app
Screens
xrandrCLI
Build a CLI
Auto completion
How to add bash auto completion:
-
To activate completions for zsh you need to have
bashcompinitenabled in zsh:autoload -U bashcompinit bashcompinit -
Afterwards you can enable completion for pipx:
eval "$(register-python-argcomplete pipx)"
taken from instructions printed to screen when running pipx completions
Links
- Essential Linux Commands
- See my file
Nice shell-commands to learn fromin this folder.
Discuss on Twitter ● Improve this article: Edit on GitHub