Linux Commands
Action | Command | ||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
COPY FILE |
// Copy a file or directory.
|
||||||||||||||||||||||||
MOVE FILE |
// Move single item.
|
||||||||||||||||||||||||
RENAME FILE |
mv oldFileName newFileName
|
||||||||||||||||||||||||
DELETE FILE |
rm my_file
|
||||||||||||||||||||||||
DELETE DIRECTORY |
// Prompts y/n.rm -r mydir // Move forcefully. rm -rf mydir
|
||||||||||||||||||||||||
DIRECTORY PERMISSION RECURSIVELY |
chmod 777 /path/to/directory // Change permission recursively. chmod -R 777 /path/to/directory
|
||||||||||||||||||||||||
ZIP DIRECTORY |
zip my_zip_file_name.zip /path/to/source -r
|
||||||||||||||||||||||||
GET ZIP FILE INFORMATION |
unzip -l my_file.zip // Directories only. unzip -l my_file.zip "*/" |
||||||||||||||||||||||||
UNZIP ZIP FILE |
unzip my_file.zip // Forcefully override files. unzip -o my_file.zip // Unzip to some directory. unzip my_file.zip -d some_directory // Unzip to some directory (Forcefully override). unzip -o my_file.zip -d some_directory // With other options. -o overwrite files without prompting -f freshen existing files, create none -n never overwrite existing files -q quiet mode (-qq => quieter) |
||||||||||||||||||||||||
EXTRACT TAR BALL |
// Unzip .TAR.GZ file.tar -xvzf my_file.tar.gz |
||||||||||||||||||||||||
VI EDITOR WRITE/QUIT |
Hit the Esc key; that goes into command mode. Then you can type..
|
||||||||||||||||||||||||
LINUX VERSION |
uname -a && cat /etc/*release |
||||||||||||||||||||||||
SHELL |
// OhMyZsh SHELL
|
#TAR BALL
ReplyDelete.TAR
Compress:
tar -cvf filename.tar file1 dir1
tar -cvf path/to/filename.tar path/to/file1 path/to/dir1
tar -cvf filename.tar path/to/dir1/subdir1 // Archive will contain path/to/dir1/subdir1 path structure.
tar -cvf filename.tar . // For compressing current directory, archive will contain ./ path structure.
tar -C path/to/dir1 -cvf filename.tar subdir1 // Using the specified directory, here archive will contain subdir1 path structure.
// Above is equivalent to- cd path/to/dir1 && tar -cvf filename.tar subdir1
Extract:
tar -C path/to/dir1 -xvf path/to/filename.tar // Extract to folder path/to/dir1
Above is equivalent to- cd path/to/dir1 && tar -xvf path/to/filename.tar
.TGZ/.TAR.GZ
Compress:
tar -cvzf filename.tgz file1 dir1
Extract:
tar -xvzf filename.tgz
.TAR.BZ2
Compress:
tar -cvjf filename.tar.bz2 file1 dir1
Extract:
tar -xvjf filename.tar.bz2
Options
- c create archive
- x extract archive
- t list contents of archive
- v verbose output
- f archive file
- z use gzip algorithm
- j use bzip2 algorithm
- C using the specified directory
CHMOD EXAMPLES:
ReplyDeletechmod [references][operators][modes] file...
chmod u=rw example.txt
chmod u-w example.txt
• REFERENCE u owner | g group | o others | a all (same as ugo)
• OPERATOR + adds | - removes | = exact modes.
• MODE r read | w write/delete | x excute-file/search-directory
Print environement variables
Deleteprintenv
printenv | grep myvar
LINUX
ReplyDelete#Identify yourself
whoami
# List files with permissions, links, owner, group, size, time, name;
ls -l
# ID of the user
# each user belongs to one or more groups but is only in one group at a time.
# the gid entry shows the user's current group.
id
id
# Group of the user
groups
groups
# List of users
Cat /etc/passwd
#Change permission
* chmod a+r foo - let all users read foo
* chmod go-r foo - stop people in the file's group, and other users from reading the file
* chmod u+x foo - make the file executable for the owner
* chmod -R a+r foodir - let all users read foodir and all the files and directories under foodir.
#change file group
* chgrp partIa1 foo - puts the foo file into the partIa1 group
* chgrp -R partIa1 foodir - puts the foodir directory and everything under it into the partIa1 group
# See latest 5 commands executed
history 5
history 5 | awk '{$1="";print substr($0,2)}' //without line number
history 5 | awk '{$1="";$2="# "$2;print substr($0,2)}' //without line number with prefix ‘# ’
# run a command recursively
watch -n 10 script.sh
-d – highlights the changes in the command output.
-n option, that specifies the interval with which the command will be executed
If you wish to hide this header, you can use the -t option
#
tail -n 15 my.log
Add sudo users here-
ReplyDeletesudo visudo
TIPS
ReplyDelete- Only ‘cd’ will take to to home directory
- Create new command alias
- Create .bash_profile file, add “alias ll=‘ls -la’”, and at last refresh shell environment
- nano .bash_profile
- source ~/.bash_profile [OR] source ~/.zsh
HARD LINKS
ReplyDeleteln /path/to/source/file /path/to/destination/link
ln /myfile.txt mylink
Now, `ls -l` will look somewhat like below, where the link column value 2 shows number of links/copies of myfile.txt
-rw-r--r--@ 2 drupal staff 363 Oct 21 22:07 mylink
Hard links have actual file contents as they share the same inode value.
Updating file content via hard link will update the source file contents as well.
Removing hard link just reduce the link count by 1, without effecting original file or other hard links of the same file.
If original file is removed, hard links retain the file content.
Creating hard link for a directory is not allowed to avoid recursive loops.
ln -h /Users/drupal/Drupal/drupal.org/modules/D8/track_progress/track_progress.info.yml yoyohl
ln -h /Users/drupal/Drupal/drupal.org/modules/D8/track_progress/track_progress.info.yml yoyohl2
-rw-r--r--@ 3 drupal staff 363 Oct 21 22:07 yoyohl
-rw-r--r--@ 3 drupal staff 363 Oct 21 22:07 yoyohl2
SOFT LINKS
ln -s /path/to/source/file /path/to/destination/link
ln -s /path/to/source/directory /path/to/destination
ln -s /myfile.txt mylink
Now, `ls -l` will look somewhat like below, with source path information
lrwxr-xr-x 1 drupal staff 79 Oct 22 00:31 mylink -> /myfile.txt
Soft or symbolic link is just a pointer to path of source file, similar to shortcut feature in Windows.
Updating file content via soft link will update the source file contents as well.
Removing soft link does not effect original file or other soft links of the same file.
But if original file is removed, the soft link become dangling link or useless.
Creating soft link for a directory is allowed.
ln -s /path/to/source/directory /path/to/destination
ln -s -f /Users/drupal/mydir /Users/beta
Now, within/inside destination 'beta' directory you can access 'mydir' directory.
Note: Relative path also works, in case it does not work- mention absolute path from root directory.
REMOVE LINKS
The rm command deletes files on Linux or Unix including a hard link.
rm link_name
Understanding Shell Script's idiom: 2>&1
ReplyDeletehttps://www.brianstorti.com/understanding-shell-script-idiom-redirect/
VI Cheat Sheet https://vim.rtorr.com/
ReplyDelete