====================================================================== Title: Shell Commands Date: 2021-08-08 Tags: box3 Link: https://spool-five.com/box3/20210808t000002--shell-commands__box3/ Word Count: 622 ====================================================================== :TODO: Clean up! -Setting default shell on linux:- - edit `/etc/passwd/` - `chsh` -`popd` and `pushd`- - can use to create a directory 'stack' - `pushd` pushes the new directory to the stack, e.g., `pushd ~/directory/another/andanother/` - then `popd` takes you back to the previous directory in the stack. - `popd +0` prints the stack (current directory first) - `popd +1` takes you to second directory in stack. -Exclamation mark `!`- - double exclamation `!!` re-run previous command - exclamation plus number `!500` run the '500th' command from history - exclamation plus a few characters `!pa` runs the recent command matching that `pacman -Sy` or whatever - any of the above followed by colon 'p' `!!:p` prints the command instead of running it. - exclamation with dollar sign `!$` use argument of previous command. - E.g., say you create a file `touch filename` then, if you run `vim !$` it will open that file in vim. Useful for very long filenames/arguments. - The `$` represents the _last_ argument of previous command. For example: `ping google.com -c 4` - `echo !$` will print `4` - `echo !^` will print the first argument instead (`google.com`) - `echo !*` will print all the arguments (`google.com -c 4`) - to get a specific number argument, you first 'select' the command, then use colon+number. E.g.: - `ping google.com -c 4` - `echo !!:2` returns `-c` if it was the previously run command. Alternatively, use `!pi`, `!389` (the history number), etc. - Also, you can use ranges: `echo !!:2-4` - Also, you can 'find and replace' text in a command using `^` e.g., - Say you run `ping googl.com -c 4` - You can correct the error in 'googl' with `^googl^google` this automatically re-runs the command. -Tail- tail -n +1 file1.txt file2.txt file3.txt Inserts filename with output `less +F ...` start less at the end of the file (for bigger files) you can also do `shift + f` to take to the end (not great on large files) -Find & Replace in Multiple Files- - `grep -rl "old_string" . | xargs sed -i 's/old_string/new_string/g'` What is happening: grep -rl: search recursively, and only print the files that contain “old_string” xargs: take the output of the grep command and make it the input of the next command (ie, the sed command) sed -i ‘s/old_string/new_string/g’: search and replace, within each file, old_string by new_string -Less - Exclude Lines from View- Once your file is open in `less` (or `journalctl`) press the following keys: - `&` - `!` - `your-exclude-keyword` Ampersand opens the pattern matching mode, exclamation mark tells `less` to exclude the following part, and then you enter your search term. -Shuf - shuffle- `-o` output (to file, for example) -Sort - sort- sorts alphabetically, use `-n` flag for numeric sorting. `-r` for reverse order `-f` ignore case Can use field separators. e.g.: sort -t : -k 3n /etc/passwd # sort using colon as delimiter, using 3rd column. `-u` unique sorting - only sort unique lines. `-o` output (to file, for example) `-R` random sorting, but groups identical keys -Uniq - unique- Usually used in conjunction with sort. Removes duplicated lines. `-u` only return unique lines `-d` only return duplicated lines `-c` provide 'count' for how many times lines occur -Search and Replace within multiple files (alternative)- I used this to replace all the footer email links in the blog directory. Seemed to work perfectly. The section included with find makes sure to skip the git directory. find . ( -type d -name .git -prune ) -o -type f -print0 | xargs -0 sed -i 's/mailto:eoin@spool-five.com/mailto:eoincarney0@gmail.com/g' -Processes- - `kill` (process id) - `pgrep` - `pstree` - `setsid` - spawn a detached child process