Linux:Tools

From Cheatsheet
Revision as of 12:15, 17 August 2023 by Patrick (talk | contribs) (→‎SSH)
Jump to navigationJump to search


Common commands

# Scroll through a file with less
less -s myfile.txt

# Select line 5 from the output
cat example.txt | sel -e '5'

# Select lines from the output, starting from the top
cat example.txt | head -5

# Select lines from the output, starting from the bottom
cat example.txt | tail -5

Uncommon commands

# Crash the current server
echo c > /proc/sysrq-trigger

Package managers

apt

# List package details and description
apt show xxx

# Show package description
apt-cache search xxx

# List installed packages
apt list

rpm

# List all local RPM packages
rpm -qa

# Query for a specific installed rpm package
rpm -qi nginx

ls

# List folders sorted by modified date
ls -trol

# List folder contents recursively
ls -alsR myfolder/

# List folder contents sorted by time, newest first and reverse order
ls -latr myfolder

# Print 9th column of folder contents
ll /mnt/btrfs/share1/ | awk '{print $9}'

tar

# Compress the destination directory and keep the source path within the zipped file
tar -czvf name-of-archive.tar.gz /path/to/directory-or-file

# Compress the destination directory, but put the folder contents into the . within the zipped file
tar -czvf name-of-archive.tar.gz -C /path/to/directory-or-file .

# Extract a tar.gz file to the current folder
tar -xzvf name-of-archive.tar.gz

find

# Basic find command
find / -name name-to-search-for

# Search the current folder for all files
find . -name \* 

# Search the current folder for all files and count them
find . -name \* | wc -l

# Search the current folder for all .log files and search & output any line containing string "error"
find . -name \*.log -exec grep -H error {} \;

# Search for all modified files between 2023-01-01 and 2023-12-30
find /var/log/ -type f -name "*" -newermt 2023-01-01 ! -newermt 2023-12-30

# Search for all modified folders between 2022-01-01 and 2022-02-10, limited to a single folders' depth
find -maxdepth 1 -type d -newermt 2022-01-01 ! -newermt 2022-02-10

# Screwing around
for i in $(find URL* -name "*.report" | sort); do echo "$i" && cat "$i" | grep TOTAL_SIZE ; done
for i in $(find URL* -name "*.report"); do basename "$i" | sort && cat "$i" | grep TOTAL_SIZE ; done
for i in $(find URL* -name "*.report"); do basename "$i" | sort && cat "$i" | grep TOTAL_SIZE | awk '{print $2}'; done
for i in $(find URL* -name "*.report"); do basename "$i" && cat "$i" | grep TOTAL_SIZE | awk '{print $2}'; done
for i in $(find * -name "*.report"); do basename "$i" && cat "$i" | grep TOTAL_SIZE | awk '{print $2}'; done
ll URL* | awk '{print $9}' | grep "*.report" | sort | for i in $(find * -name "*.report"); do basename "$i" && cat "$i" | grep TOTAL_SIZE | awk '{print $2}'; done
ll URL* | awk '{print $9}' | grep .report | sort | for i in $(find * -name "*.report"); do basename "$i" && cat "$i" | grep TOTAL_SIZE

find URL1 -name \*.report -exec grep -H TOTAL_SIZE {} \; | LC_ALL=C awk -M 'BEGIN{FS=OFS="\t"} {printf("%s\t%.02f\n", $1, $2/(1024*1024*1024))}' | sed -e 's~^.*/~~' -e 's~\..*SIZE~~' | sort

Less

25 = Go to line 25
G = Go to bottom of file
/ = Activate search mode
/Error = Search for "Error"
n = Move to next search result
N = Move to previous search result
# Don't wrap long lines to the current screen (move left or right to see truncated line)
less -S /var/log/syslog

# Output a file's contents and read it with less
cat /etc/snmpd/snmp.conf | less -S

# Number the lines when viewing
less -N /var/log/messages

# Open less at the first search result. (Do not use space between the -p parameter and your search query)
less -p"Error" /var/log/messages

SSH

# Execute 'ls' on a remote server and output the result to your shell session
echo 'ls' | ssh -T user@10.0.20.75

# Execute a command on a remote server and output the result to a local file
echo 'ls' | ssh -T user@10.0.20.75 > <filename>.log

# Log in by providing a password in the CLI
sshpass 'MyPassword' ssh -XY root@10.100.25.1

mdtest

This chapter was written and contributed by Ivo Palli.

General

mdtest is part of the ior performance test package.

Installation

wget https://github.com/hpc/ior/releases/download/3.3.0/ior-3.3.0.tar.bz2
tar xjf ior-*.tar.bz2
cd ior-*/

yum install openmpi-devel environment-modules
# Relog your shell so 'module' is available
module load mpi
module list
./configure
make -j4
make install

Usage

Note: Number of items should be a multiple of depth x branching factor

module load mpi
mpirun --oversubscribe --allow-run-as-root -n 10 mdtest -n 2000 -z 5 -b 2 -d /mnt/nfs

Links