Skip to content

Instantly share code, notes, and snippets.

@Synkevych
Last active February 1, 2022 13:00
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Synkevych/bbb2f2c8fdf9c849c65f346802f2ead8 to your computer and use it in GitHub Desktop.
Save Synkevych/bbb2f2c8fdf9c849c65f346802f2ead8 to your computer and use it in GitHub Desktop.
Bash Commands and Tips for Beginners

Info about the system

whoami current user name
hostname current pc name
cat /etc/os-release - info about current system getent group sudo | cut -d: -f4 - show all sudo users
su - user2 - switch between users
sudo chmod a+rwx folderName/* add all access to the folder
chown -R username directory - add full permission directory and to all files and directories in that directory
chowd -R u+rX directory - the same as previous

General information about folders and files

du -h show folder size with human readable mode
du -ch folder_name/ show folder size with total
du ~/go | sort -n -r | less show sorted folder name
du folder_name/ | sort -n -r | head -n 20 show only 20 sorted files
ls -1 | wc -l count total files in directory
ls | head -23 limit the number of files printed by ls
ls [a-l]* – list files that start with a letter from a to l
ls *.html *.js - list files with certain extensions
sort dogs.txt | uniq -d – show only duplicates
ln <original> <link> - create a hard link, the link will still contain the original file content even if you delete original file
ln -s <original> <link> – create soft link, if you remove original file it will broke the link
find . -name "*.js" | xargs du -sch - show size of all .js file in a current directory tree

Work with a file or part of it

locate -c file_name.txt - find file and count res
locale file_name.txt - find file by name
cat file1 file2 >> file3 – append the content of multiple files into a new file
cat -n file1 - see the line numbers(also, -s, -b options)
less <filename>- /[name] - searches foreword, ?[name] - searches backwards
tail -f /var/log/system.log – watches for file changes
tail -n 10 <filename>- print the last 10 lines
diff file1 file2 - [-y compare line by line][-u diff between version ] show difference of two files
grep -n document.getElementById index.md - find name in file
wc -l test.txt - count the line inside file
rm -rf * - remove all folders and files inside current folder rm -rf folderName - delete a folder and its contents recursively cp -r folderName newName - copy a folder and its contents recursively less +G logfile - open the file and scroll to the end, convenient for logs that are added in real time
rename "s/netcdf_zip$/GRIB/" multi_level* - rename a bunch of files

Zip and Unzip files

gzip filename – compress the file, and append a .gz extension to it, the original file is deleted.
gzip -k filename – compress the file and leave orig. file intact
gzip -d filename.gz – decompress file

tar -cf archive.tar file1 file2 - create archive from files
tar -xf archive.tar -C directory – extract to a specific folder
tar -czf archive.tar.gz file1 file2 – create a gzipped archive
tar -xf archive.tar.gz – unzip archive

Work with Vim

:101 - go to the line 101
G - to the end of document
gg - to the top of document
E - to the end of line
0 - to start of the line
:%d - delete all content
ZZ - in show mode save the file and exit from Vim
:%s/.txt/.txt_copy/g replace file extension from [.txt] to [.txt_copy]
:%s;/home/lib;/home/roman/lib;g - replace some long path :set number - show line number from the left
:set nonumber - remove number line from the left
?Word - searching for the Word in the document [N or n for next result]
Ctrl + V - Visual block mode [d - delete all the selected text]
:s/foo/bar/gc - replace words foo to bar , c - confirm each substitution
:s/\<foo\>/bar/ - Substituting Whole Word

@proplayer99
Copy link

Great post. It will definitely be useful to me.

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