Create, Delete and Move Zipfiles and Directories
To add a directory
MKDIR </directoryname>
To add a directory with intermediate directories
MKDIR </directoryname/sub-directoryname/sub-directoryname>
(Add -p to continue if directory already exists)
To remove a directory that contains other files or directories with prompts use the following command
rm -r <directoryname>
To remove a directory that contains other files or directories without prompts use the following command
rm -rf <directoryname>
To remove an empty directory use the following command.
rmdir <directoryname>
Options:
--ignore-fail-on-non-empty: Ignore any failure which occurs solely because a directory is non-empty.
-p: Each directory argument is treated as a pathname of which all components will be removed, if they are empty, starting with the lastmost component.
-v, --verbose: Display verbose information for every directory processed.
--help: Display a help message, and exit.
--version: Output version information, and exit.
rmdir -p </directoryname/sub-directoryname>
removes subdirectory first and then directory
Hidden Directories or Files (name preceded by .)
.directory
.filename
To Move files between directories
sudo mv <directoryname including path if necessary>/filename <new directoryname including path if necessary>/filename
To Copy files from remote server to local machine
sudo scp remoteusername@host:/path/filename-to-be-copied /path/to/local/save-as/location
To Copy files from local machine to remote server
sudo scp /path/filename-to-be-copied remoteusername@host:/path/to/local/save-as/location
To Delete files
rm [-f|i|I|q|R|r|v] filename
rm removes a file if you specify a correct path for it and if you don't, then it displays an error message and move on to the next file.
Sometimes you may not have the write permissions for a file, in that case it asks you for confirmation. Type yes if you want to delete it.
Options
-f - deletes read-only files immediately without any confirmation.If both -f and -i are used then the one which appears last in the terminal is used by rm.
-i - prompts for confirmation before deleting every file beforing entering a sub-directory if used with -R or -r. If both -f and -i are used then the one
which appears last in the terminal is used by rm.
-q - suppresses all the warning messages however error messages are still displayed. However the exit status is modified in case of any errors.
-R - means delete recursively and is used to delete the directory tree starting at the directory specified i.e. it deletes the specified directory
along with its sub-directory and files.
-r - same as -R.
-v - displays the file names on the output as they are being processed.
-I - prompts everytime when an attempt is made to delete for than 3 files at a time or while removing recursively. "
Precautions
Never type sudo rm -R / or sudo rm -r / as it deletes all the data in the root directory and will delete the data of all the mounted volumes unless
you want to wipe out everything from your system.
sudo rm -f /* also does blunders with your system.
NOTE: It is possible to recover files deleted through rm. If you want to permanently delete a file use shred.
Expand a Tar.Gz file
tar -xzf <filename>.tgz
Options
f: this must be the last flag of the command, and the tar file must be immediately after. It tells tar the name and path of the compressed file
z: tells tar to decompress the archive using gzip
x: tar can collect files or extract them. x does the latter
v: makes tar talk a lot. Verbose output shows you all the files being extracted
Expand a Tar.Gz to a particular locationpdw
tar -xzf <filename>.tgz -C /directory/subdirector
Create a Tar.Gz file in a particular location
tar czf /path/<filename.tgz> /path to item to be compressed/ (just use . to compress current directory)
To add HOSTNAME to generated files
"filename_`echo $HOSTNAME`.<ext>"
Create Alias
One-time use in current Terminal window
alias aliasname='command -options'
Permanent use in all Terminal window
vi ~/.bashrc
Add to bottom of file
alias aliasname='command -options'
Save
Execute Change
. ~/.bashrc
Command Management:
Combined Commands Options
A; B Run A and then B, regardless of success of A
A && B Run B if A succeeded
A || B Run B if A failed
A & Run A in background
To View Documents
Use cat if your file is short
cat filename
Use less if you have to view the contents of a longer file
(see LESS section for more details)
less filename
You can make less behave like cat when invoked on small files and behave normally
otherwise by passing it the -F and -X flags.
less -FX filename
Use od if your file contains strange or unprintable characters
$ cat file
(ÐZ4 ?o=÷jï
$ od -c test
0000000 202 233 ( 320 K j 357 024 J 017 h Z 4 240 ? o
0000020 = 367 \n
0000023
To Modify Documents
Use VIM (also VI)
VIM filename
Press ESC to obtain the command prompt within VIM
:q to quit (short for :quit)
:q! to quit without saving (short for :quit!)
:wq to write and quit (think write and quit)
:wq! to write and quit even if file has only read permission
(if file does not have write permission: force write)
:x to write and quit (shorter than :wq)
:qa to quit all (short for :quitall)
:v (to copy) arrowing down will highlight content, Y to "yank" data
Placing cursor where you want it and typing P will "paste" yanked data
When you press : a : will appear at the bottom of the screen.
Or you can press Esc ZZ (Esc Shift+Z Shift+Z) to write/save if the file was changed, then quit.
Or if you don't want to save changes you can use ZQ instead.
To access VIM help
Type Esc
:help
Return and you will have all your answers and even a neat tutorial.
To set VIM defaults
VIM .vimrc
LESS/Vi Editor How-Tos
Open File: less filename or less /path/filename
Search: /keyword (searches forward through document)
Delete Line: dd
Delete Multiple Lines: #dd
Insert Line: o
Undo: u
Highlight: V+Down Arrow
Cut: d
Paste: p
Show Line Numbers: set number
Comment Out Line: <Line#>s/^/#
Uncomment Out Line: <Line#>s/^#/
Comment Out Range of Lines: <Line#,Line#>s/^/#
Uncomment Out Range of Lines: <Line#,Line#>s/^#/
Delete (by Character): x
Save: w
Quit: q (:q! to force quit w/o writing)
Save and Quit: wq or ZZ
To View Document/File/Directory Attributes
ls -l
Alias ll will do the same if it is enabled
Attributes
User Group Everyone
-rwx rwx rwx
r = 4
w = 2 rwx = 7 (4+2+1) r-x = 5 (4+1) rw- = 6 (4+2) --- = 0
x = 1
Example:
-rwxr-xr-- = 754
-rwxrwxrwx = 777
-rwxrwx--- = 770"
To Modify Attributes of a directory (filename)
sudo chmod XXX filename
replace XXX with calculation from above
To Modify Attributes of a specific file
Browse to directory (cd /directoryname/sub-directoryname/)
sudo chmod XXX picture1.png
replace XXX with calculation from above
To View Document/File/Directory Details
file filename
To search directory files’ contents
grep searchword directory/* (searches directory specified)
grep searchword * (searches current directory)
grep searchword filename (searches specified file)
grep -r searchword . (searches current directory and subdirectories)
grep -r --included="searchword or *.<extension>" .
(searches current directory and subdirectories for the searchword or specified variable)
To create new files
CD to desired directory and enter the following command
touch filename.extension
To extract the first * of lines from a file into a separate file
(good for parring down log files) use the following: head -n 100000 /opt/symas/logs/slapd_log.log > tmp.log