Skip to content

Instantly share code, notes, and snippets.

@mbodo
Last active July 13, 2020 19:27
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mbodo/4705709a098fa985b53e185a98bbcea7 to your computer and use it in GitHub Desktop.
Save mbodo/4705709a098fa985b53e185a98bbcea7 to your computer and use it in GitHub Desktop.
Linux - Commands cheatsheet

Linux - Commands cheatsheet.md

usermod

Remove all secondary groups for user

  • RHEL 7
usermod -G "" user

find

Rename all files and directories from broken GID 2000 to group "foo"

find / -group 2000 -exec chgrp -h foo {} \;

Find files biggest than +100MB (RHEL/CentOs)

find /var/log -type f -size +100000k -exec ls -lh {} \; | awk '{ print $9 ": " $5 }'

/var/log/messages.1: 5G
/var/log/messages: 4G

Find *.bat files and remove them

find folder/ -type f -name "*.bat" -exec rm -f {} \;

Find user only files without include parent directory (in this case folder/)

find folder/ -maxdepth 1 -type f -user <user> -print0 | xargs -0 ls -l

Find rename multiple files with perl script

find . -type f -name "*bar*" | xargs -n 1 /usr/bin/perl -e '($new=$ARGV[0]) =~ s/bar/foo/; system(qq(mv),qq(-v), $ARGV[0], $new);'

Find files not owned by user

find . \! -user foo -print

Find with excluding one directory

will exclude: ./misc

find . -path ./misc -prune -o -name '*.txt' -print

Find with excluding multiple directories

will exclude: dir1 dir2 dir3

find . -type d \( -path dir1 -o -path dir2 -o -path dir3 \) -prune -o -print

(how-to-exclude-a-directory-in-find-command) [https://stackoverflow.com/questions/4210042/how-to-exclude-a-directory-in-find-command]

Find numerical chmod permissions

find . -printf "%m:%f\n"

650:.

du

Find the largest files and directories (in this case top 10)

du -hsx * | sort -rh | head -10

List size of directories

sudo du -hx --max-depth=1 /var

4.0K    /var/nis
82M     /var/spool
...

cat

Clean a log file without delete it

cat /dev/null > logfile

How to clean log files (http://unix.stackexchange.com/questions/92384/how-to-clean-log-file)

tar

Create tar.gz archive from current folder and omit the excludes

.
..
example1
example2
example3

tar --exclude="./example1" --exclude="./example2" --exclude="backup_example.tar.gz" -cvzf backup_example.tar.gz .

will appear as

tar -tvzf backup_example.tar.gz .

.
..
example3

Create tar.gz archive from last folder in path and save to current path

tar -cvzf archive.tar.gz -C /home/user/some_folder .

will create:

./archive.tar.gz

Create tar.gz archive in different directory

To input compressed package to another directory, just define where (e.g /var/www/{compressed_package}.tar.gz)

tar -cvzf /var/www/backup_example.tar.gz  some_folder_or_file

compressed package will appear in:
/var/www/

Untar tar.gz archive skipping root folder

we have backup_example.tar.gz with this structure

.
..
/root
|
-/folder1
-/folder2

tar -xvzf backup_example.tar.gz --strip 1

will untar as

/folder1
/folder2

Untar tar.gz to different directory

tar -xzvf backup_example.tar.gz  -C /var/www

compressed package will be unpacked to:
/var/www/

zip

Zip recursively directory

zip -r directory.zip directory

unzip

Unzip to new folder and exclude some subdirectories from zip archive

Let's have some zip archive:

|example.zip
|-folder1
|-folder2

unzip example.zip -x "folder1/*" -d example_unziped

Output:

|example_unziped
|-folder2

works also with wildcards

|example.zip
|-folder1a
|-folder2
|-folder3a

unzip example.zip -x "folder*a/*" -d example_unziped

Output:

|example_unziped
|-folder2

Unzip to new folder and include only one directory

Let's have some zip archive:

|example.zip
|-folder1
|-folder2

unzip example.zip "folder1/*" -d example_unziped

Output:

|example_unziped
|-folder1

Unzip multiple files

a.zip
b.zip
c.zip

$ unzip '*zip'


Archive:  c.zip
   creating: c/
  inflating: c/test.txt

Archive:  a.zip
   creating: a/
  inflating: a/test.txt

Archive:  b.zip
   creating: b/
  inflating: b/test.txt

3 archives were successfully processed.

cron

Export cron jobs

crontab -l > cron.bak

Import cron jobs

crontab cron.bak

chown

Change symbolic link ownership

chown -vh user:group symbolic_link 

do not include /, like symbolic_link/ , use plain symbolic_link

Change symbolic link ownership

ps

Show how long is process running

ps -o etime= -p "$$" 

where:

$$     - PID of process
etime= - Skip header

return format: [[dd-]hh:]mm:ss

how-to-check-how-long-a-process-has-been-running

curl

Get resource with authentication

curl -u username:password http://location/*.html

Get href links from resource with authentication

curl -u username:password http://location/*.html | grep "<a href=" | sed "s/<a href/\\n<a href/g" | sed 's/\"/\"><\/a>\n/2' | grep href | sort

You should see:
<a href="link1"></a>
<a href="link2"></a>
<a href="link3"></a>
..etc

easiest-way-to-extract-the-urls-from-an-html-page-using-sed-or-awk-only

vimdiff

Diff local file and remote file

vimdiff /path/to/file scp://user@remotehostip//path/to/file

root.cz - pouziti-vimu-a-jeho-pluginu-pro-porovnavani-a-slucovani-souboru

diff

Diff two directories

diff -r dir1 dir2 | grep dir1 | awk '{print $4}' > difference1.txt

Explanation:

  • diff -r dir1 dir2 shows which files are only in dir1 and those only in dir2 and also the changes of the files present in both directories if any.
  • diff -r dir1 dir2 | grep dir1 shows which files are only in dir1
  • awk to print only filename.

Diff two directories short version

diff -qr dir1 dir2

Files dir1/file1 and dir2/file1 differ
Only in dir1: file2
Only in dir2: file3

difference-between-two-directories-in-linux

root.cz - nastroje-pro-porovnani-obsahu-dvou-textovych-souboru

touch

Changing the timestamp of a symlink

touch -h -t 201301291810 myfile.txt
            [[CC]YY]MMDDhhmm[.ss]

Mandatory arguments to long options are mandatory for short options too.
  -a                     change only the access time
  -c, --no-create        do not create any files
  -d, --date=STRING      parse STRING and use it instead of current time
  -f                     (ignored)
  -h, --no-dereference   affect each symbolic link instead of any referenced
                         file (useful only on systems that can change the
                         timestamps of a symlink)
  -m                     change only the modification time
  -r, --reference=FILE   use this file's times instead of current time
  -t STAMP               use [[CC]YY]MMDDhhmm[.ss] instead of current time

changing-the-timestamp-of-a-symlink

ip

Get ip address only (replace eth0 whith your real IF)

ip addr show eth0 | grep "inet\b" | awk '{print $2}' | cut -d/ -f1
$ 192.168.XXX.XXX

displaying-ip-address-on-eth0-interface

/proc

ip address from /proc/net/fib_trie

awk '/32 host/ { print f } {f=$2}' <<< "$(</proc/net/fib_trie)"

how-to-get-the-ipv4-address-for-an-interface-from-proc

pam_tally2

Reset count of attempt access for user

pam_tally2 -u your_user --reset

cant-unlock-linux-user-account

Display login failures count for user

pam_tally2 --file /var/log/tallylog

Login           Failures Latest failure     From
root                3    0X/XX/XX XX:XX:XX  pts/1
apache              1    0X/XX/XX XX:XX:XX  pts/2

xargs

Use xargs to display process command line arguments

xargs -0 printf '%s\n' < /proc/14805/cmdline

-server
-Xms256m
-Xmx512m
-XX:MaxPermSize=512m
...

Use xargs to display environment variables

xargs --null --max-args=1 echo < /proc/<PID>/environ

xargs-examples

sed

Use sed to replace URL with # delimiter

echo 'url=' | sed "s#url=#http://localhost:80#g"

url=http://localhost:80

how-to-use-sed-to-find-and-replace-url-strings-with-the-character-in-the-tar

grep

Exclude specific files from search

grep -iR --color '<pattern>' --exclude=\*.{log*,out*} .
   
will exclude files with *.log, *.out filetypes

date

Change only date and preserve time

date -s "$(date +'%Y12%d %H:%M')"
Mon Dec 22 10:55:03 GMT 2014

changes the month to the 12th month - December. Time si preserved.

linux-set-date-through-command-line

dig

dig-command-examples

ss

List all unique ports

sudo ss -plnt | awk '{ print $4 }' | cut -d ']' -f 2 | cut -d ':' -f 2 | sort -n | uniq

TODO:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment