Indispensable Linux Commands and Statements - Davz33/tutorials GitHub Wiki
first 9 lines: head -n 9 <filepath>
last 9 lines: tail -n 9 <filepath>
find <path> --name <name>
you can also specify -type f
to restrict the search to file types.
find . --name <name>
find / --name <name>
note: careful with this one as it might bloat (and get stuck) your entire OS.
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:
<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
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
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>
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.
..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>
userdel <username>
getent groups
sudo -l
chmod --reference=source target
chown --reference=source target
using the ampersand (&)
<command> &
First press Ctrl+Z, then
bg
you can then list all background jobs via
jobs
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)