Indispensable Linux Commands and Statements - Davz33/tutorials GitHub Wiki

General

Read first and last n lines

first 9 lines: head -n 9 <filepath>
last 9 lines: tail -n 9 <filepath>

Find file or directory in folder

find <path> --name <name>
you can also specify -type f to restrict the search to file types.

find a file or directory in current path

find . --name <name>

find a file or directory everywhere

find / --name <name>

note: careful with this one as it might bloat (and get stuck) your entire OS.

Find line in file containing / matching string, limit search to first (last) n lines

grep -e "<matching string>" -n -A 0 <filepath>
Here -A 0 is an explicated default flag. With a different integer value than 0, you'll be able to print a number of lines before or after the matching string.
Let's see a more complex use-case as well:

Loop through paths within file and grep for each

<head / tail> -n < positive or neg integer>` <filepath> | while read -r line; do  
  grep -e "<matching string>" -n -A 0 $line  
done

The beauty of this approach, is you're able to pre-emptively store the search path in a text file, line by line, then decided on a subset of it using head -n and tail -n at run time, and finally, look for your string in each of the files. Furthermore you're also able to add subdirectories or edit the path within your loop statement, e.g. $line/config. You could also wrap that in a function:

searchAll ()
{

}

and dump the result to a newer text file via: searchAll <args> >> results.txt

Use-case

Let's pretend I look for a particular config line (all the remote origin) across all of my .git local repositories.
I've already created a .tempfind.temp with the result of a find statement, whereby each line is the absolute path to a .git folder.
After feeding the the matching (escaped) string to grep -e, I need to grab the line after the matching string, which will contain the URL: -A 1. Finally, I'll add the last bit to the the grep path, in order to look within the config sub-directories only:

head -n 9 /root/.tempfind.temp | while read -r l; do
  grep -e "\[remote \"origin\"\]" -A 1 $l/config
done

Delete the last command in your history

Why? You might want to keep your $HISTFILE clean to swiftly go back and re-run previous commands. By deleting erroneous statements and typos, you'll have an easier time doing it
history -d -2

More generally, to delete the last n statements:
history -d -<n+1>

Separate HISTORY files for same user

you can have separate hist..

You can have separate history files in Linux by setting the HISTFILE environment variable for each terminal session.
This can be done in your .bashrc file or by using the bash command-line option -H. For example, if you want to set the history file for a session to be in the directory ~/data_related_commands_hist, you can use the command:

bash -H -c "export HISTFILE=~/data_related_commands_hist/.bash_history"

The history file will be placed in the ~/data_related_commands_hist, and all commands run in that session will be written to the file. You can also use the HISTIGNORE variable to ignore certain commands from being written to the history file.

Access

Create new user with sudo privileges (sudoer)

..and set root's home directory as the new user's home directory
useradd -d $HOME -g sudo <username>

to assign a new home directory to the user:
useradd -m [-d <path>] -g sudo <username>

Delete user

userdel <username>

List all user groups

getent groups

List all users and their associated privileges

sudo -l

Copy file permissions to another file

chmod --reference=source target

Copy file ownership to another file

chown --reference=source target

Processes

Run a command in the background

using the ampersand (&)

<command> &

Send a running command to the background

First press Ctrl+Z, then
bg
you can then list all background jobs via
jobs

Run command with limited number of CPUs

If you want to run a command on a specific core, you can use the following command:

taskset -ac 1 my_command

This will run the command "my_command" on core 1. If you want to run the command on multiple cores, you can specify a comma-separated list of cores. For example:

taskset -ac 1,2,3 my_command

This will run the command on cores 1, 2 and 3.

-c stands for --cpu-limit
-a tells taskset to apply restrictions to children processed (e.g. forked processes)

⚠️ **GitHub.com Fallback** ⚠️