Linux Flashcards
Check memory usage
free -h shows all free memory
du -h -d 1 (disk usage, human, depth =1) meaning the depth of folders it will look.
df -h shows how much memory each directory is consuming in human readable form
Du -a -h -d 1 shows you all the files, hidden files, and directories disk usage
Find text in a directory
grep /path/to/file -e text-to-search
grep /path/to/dir -e text-to-search
grep -rnw /path/to/dir -e text-to-search
- r or -R is recursive,
- n is line number, and
- w stands for match the whole word.
- l (lower-case L) can be added to just give the file name of matching files.
- e is the pattern used during the search
Read file to console
cat
Open file to edit
vim
In vim
quit file, save quit, edit file, how to stop editing
:q!, :q, :wq, [i] for insert, escape to stop editing
print files in dir
ls, ls -al shows all hidden files with permissions
move file, make directory, remove directory, make file
mv file1 file2
mkdir dirName
rmdir dirName
touch file
find a file
find /dir/ -name file.txt
searches for the file in the dir and sub dirs
read beginning or end of files
head -n file.txt
tail -n file.txt
See differences between 2 files
diff file1 file2
remove file, remove directory, remove directory and all sub files/dirs
rm file, rmdir dirname, rm -rf dirname
create a tar file, unzip a tar file
tar -cvf name.tar dirname/or/filename
- c create tar
- v verbose
- f specify file name
tar -xzf tar.gz
- x extract
- z gzip, gunzip, ungzip,
- f file
change read-write permissions, change owner
chmod filename
chown newowner:newgroup filename
show all jobs running and their statuses. Jobs are started by the shell
jobs
stop a process
kill -9/-15 PID
download a file given a link
wget download-link
check linux system information such as machine name, operatin system, kernel
uname
cat /proc/cpuinfo
task manager but for linux
top
see a list of previous commands run
history
get a list/help with linux commands
man
check ip address
ip addr
hostname -i
how to copy a file, directories, while preserving file state?
cp file1 file2 – copies file1 to file2
cp file1 file2 new – copies file1/file2 into new dir
cp -R dir1 dir2 – copies all files from dir1 into dir2. If dir2 exists it will make a subdir of dir1 in dir2, if it does not exist it will create dir2
-p option preserves all information about the file, time of last edit, permissions, ect
monitor a process on the system
ps aux | grep
do 2 tasks at once in a single line
use &&
cd /home && ls - al
cd’s to home and executes ls -al
how to use scp on, copy a file from a remote location or copy to a remote location
Linux file -> windows
Scp user@domain:
Windows -> linux
Scp user@domain:
**to go from linux to windows, you need to set up an scp server on windows. This is not default
check network traffic
tcpdump
tcpdump -i any
or
there are many ways to use this command
list of linux file permissions you can set
example:
chmod filename
400: Gives the user read permission, and removes all other permission. These permissions are specified in octal, the first char is for the user, second for the group and the third is for other
755: means read and execute access for everyone and also write access for the owner of the file. … So, there should be no permission to everyone else other than the owner to write to the file, 755 permission is required.
sed, stream editor command
https://www.geeksforgeeks.org/sed-command-in-linux-unix-with-examples/
$cat > geekfile.txt
unix is great os. unix is opensource. unix is free os.
learn operating system.
unix linux which one you choose.
unix is easy to learn.unix is a multiuser os.Learn unix .unix is a powerful.
Sample Commands
Replacing or substituting string : Sed command is mostly used to replace the text in a file. The below simple sed command replaces the word “unix” with “linux” in the file.
$sed ‘s/unix/linux/’ geekfile.txt
this replaces the first ‘unix’ per line with ‘linux
$sed ‘s/unix/linux/g’ geekfile.txt
makes replacements for all found characters
$sed ‘s/unix/linux/3g’ geekfile.txt
replaces the 3rd