Amazon

Monday, May 6, 2019

Managing files from the command line

File system hierarchy

/ - This is the system's root directory.
/root - This is the root account's home directory
/home - User home directories are located under this directory.
/etc - This directory contains static, persistent system configuration data.
/var - This directory contains dynamic configuration data, such as FTP and websites.
/usr/bin - Regular user commands and utilities are located here.
/usr/sbin - System administration binaries, for root user, are here.
/tmp - Temporary files are stored here.
/run - Contains dynamic, non-persistent application runtime data.
/usr - Contains installed software programs and libraries.

Locating Files and Directories

ls -l ~ - List the current user's home directory (long format) in simplest syntax, when it is not the current location.
cd - Return to the current user's home directory.
pwd - Determine the absolute path name of the current location.
cd - -Return to the most previous working directory.
cd ../.. - Move up two levels from the current location.
ls -al - List the current location (long format) with hidden files.
cd /bin - Move to the binaries location, from any current location.
cd.. - Move up to the parent of the current location.
cd bin - Move to the binaries location, from the root directory.

Path Name Expansion 

b* - Only filename beginning with "b"
*b - Only filenames ending in "b"
*b* - Only filenames containing a "b"
[!b*] - Only filenames where first character is not "b"
???* - Only filenames at least 3 characters in length
*[[:digit:]]* - Only filenames that contain a number
[[]:upper:]* - Only filenames that begin with an upper-case letter

Navigating paths

pwd - The pwd command display the full path name of the current location.

E.g. [student@desktop ~]$ pwd
/home/student

ls - The  ls command lists directory contents for the specified directory or, if no directory is given, for the current directory.

E.g. [student@desktop ~]$ ls
Desktop   Documents   Download   Music   Pictures 
Public   Templates   Videos

cd - use the cd command to change directories.With a working directory of /home/student, relative path syntax is shortest to read the Videos subdirectory. The Documents subdirectory is then reached using absolute path syntax.

E.g.
[student@desktop ~]$ cd Videos
[student@desktop Videos ~]$ pwd
/home/students/Videos
[student@desktop Videos ~]$ cd /home/student/Documents
[student@desktop Documents ~]$ pwd
/home/student/Documents
[student@desktop Documents ~]$ cd
[student@desktop ~]$ pwd
/home/student

touch - The touch command normally updates a file's timestamp to the current date and time without otherwise modifying it. This is useful for creating empty files, which can be used for practice, since "touching' a file name that does not exist caused the file to be created. Using touch, practice files are created in the Documents and Videos subdirectories.

E.g.
[student@desktop ~]$ touch Videos/blockbuster1.ogg
[student@desktop ~]$ touch Videos/blockbuster2.ogg
[student@desktop ~]$ touch Documents/thesis_chapter1.odf
[student@desktop ~]$ touch Documents/thesis_chapter2.odf

ls - The ls command has multiple options for displaying attributes on file. The most command and useful are -l (long listing format), -a (all files, includes hidden files), and -R(recursive, to include the contents of all subdirectories).

E.g.
[student@desktop ~]$ ls -l

[student@desktop ~]$ ls -a

[student@desktop ~]$ ls - R

cd - 

E.g.
[student@desktop ~]$ cd Videos
[student@desktop Videos ~]$ pwd
/home/student/Videos
[student@desktop Videos ~]$ cd /home/student/Documents
[student@desktop Documents ~]$ pwd
/home/student/Documents
[student@desktop Documents ~]$ cd -
[student@desktop Videos ~]$ pwd
/home/student/Videos
[student@desktop Videos ~]$ cd -
[student@desktop Documents ~]$ pwd
/home/student/Documents

cd.. - 
E.g.
[student@desktop Videos ~]$ pwd
/home/student/Videos
[student@desktop Videos ~]$ cd .
[student@desktop Videos ~]$ pwd
/home/student/Videos
[student@desktop Videos ~]$ cd . .
[student@desktop ~]$ pwd
/home/student
[student@desktop ~]$ cd . .
[student@desktop home ~]$ pwd
/home
[student@desktop home ~]$ cd . .
[student@desktop / ]$ pwd
/
[student@desktop / ]$ cd
[student@desktop ~]$ pwd
/home/student

Create Directory

mkdir - The mkdir command creates one or more directories or subdirectories, generating error if the file name already exists or when attempting to create a directory in parent directory that doesn't exit. The -p parent option creates missing parent directories for the requested destination. be cautious when using mkdir -p, since accidental spelling mistake create unintended directories without generating error message.

E.g.
[student@desktop ~]$ mkdir Video/Watched
mkdir: cannot create directory Video/Watched : No such file or directory.

The mkdir failed because Videos was misspelled. "Video"
does not exist as a location in which to create the Watched subdirectory. If -p were used, the user would not have received an error message and now have two directories, Video and Videos.

[student@desktop ~]$ mkdir Videos/Watched
[student@desktop ~]$ cd Documents
[student@desktop ~]$ mkdir ProjectX ProjectY
[student@desktop ~]$ mkdir -p Thesis/Chapter1 Thesis/Chapter2 Thesis/Chapter3
[student@desktop ~]$ cd
[student@desktop ~]$ ls - R Videos Documents

The last mkdir create three ChapterN subdirectories with one command. The -p parent option created the missing parent  directory Thesis.

copy file cp - The cp command copies one or more files to become new, independent file. In any destination, new file names must be unique. If the new file name is not unique, the copy command will overwrite the existing file.

E.g.
[student@desktop ~]$ cd Videos
[student@desktop ~]$ cp blockbuster1.ogg blokbuster3.ogg
[student@desktop ~]$ ls -l

Move files mv - The mv command renames file in the same directory, or relocates files to a new directory.

E.g.
[student@desktop Videos ~]$ cd .. /Documents
[student@desktop ~]$ ls - l
[student@desktop Documents ~]$ mv thesis_chapter2.odf thesis_chapter2_reviewed.odf
[student@desktop Documents ~]$ mv thesis_chapter1.odf Thesis/Chapter1
[student@desktop Documents ~]$ ls -lR

The first mv command is example of remaining a file. The second causes the file to be relocated to another directory.

Remove files and directories rm - Default syntax for rm deletes files, but not directories. Deleting a directory, and potentially many subdirectories and files below it, requires the -r recursive option. There is no command-line undelete feature, nor a trash bin from which to restore.

E.g.
[student@desktop Documents ~]$ pwd
/home/student/Documents
[student@desktop Documents ~]$ rm thesis_chapter2_reviewed.odf
[student@desktop Documents ~]$ rm Thesis/Chapter1
rm cannot remove 'Thesis/Chapter1' :Is a directory
[student@desktop Documents ~]$ rm -r Thesis/Chapter1
[student@desktop Documents~]$ ls -l Thesis   
[student@desktop Documents ~]$ ls -ri Thesis
press 'y' to all queries

Using -i will interactively prompt for each deletion. This is essentially the opposite -f which will force the deletion without prompting the user.

The rmdir command deletes directories only if empty. Remove directories cannot be undeleted

E.g.
[student@desktop Documents ~]$ pwd
/home/student/Documents
[student@desktop Documents ~]$ rmdir ProjectY
[student@desktop Documents ~]$ rmdir ProjectX
rmdir failed to remove 'ProjectX' : Directory not empty
[student@desktop Documents ~]$ rm -r ProjectX
[student@desktop Documents ~]$ ls -lR

The rmdir command failed to delete non-empty ProjectX, but rm - r succeeded.

Pattern Matching

A sample set of files is useful to demonstrate expansion.

E.g.
[student@desktop ~]$ mkdir glob; cd glob
[student@desktop  glob ~]$ touch alfa bravo charlie delta echo able baker cast dog easy
[student@desktop glob ~]$ ls

First, simple pattern matching using * and ?.
[student@desktop glob ~]$ ls a*
able alfa
[student@desktop glob ~]$ ls *a*
able alfa baker bravo cast charlie delta easy
[student@desktop glob ~]$ ls [ac]*
able alfa cast charlie
[student@desktop glob ~]$ ls ????
able alfa cast easy echo
[student@desktop glob ~]$ ls ?????
baker bravo delta

Tilde expansion - The tilde character (~), when followed by  a slash delimiter, matches the current user's home directory.
When followed by a string of characters up to a slash,it will be interpreted as a username. If one matches. If no username matches, then an actual tilde followed by the string of characters will be returned.

E.g.
[student@desktop glob ~]$ ls -/glob
able alfa baker bravo cast charlie delta dog easy echo
[student@desktop glob ~]$ echo -/glob
/home/student/glob

Brace expansion - Brace expansion is used to generate discretionary string of characters.

E.g.
[student@desktop glob ~]$ echo {Sunday, Monday, Tuesday, Wednesday}.log
Sunday.log Monday.log Tuesday.log Wednesday.log
[student@desktop glob ~]$ echo file{1..3}.txt
[student@desktop glob ~]$ echo file{a..c}.txt
[student@desktop glob ~]$ echo file{a,b}{1,2}.txt
[student@desktop glob ~]$ echo file{a{1,2}b,c}.txt

Command substitution - Command substitution allows the output of a command to replace the command itself.
The $(command) from can nest multiple command expansion inside each other

E.g.
[student@desktop glob ~]$ echo Today is 'date +%A'
Today is Tuesday
[student@desktop glob ~]$ echo The time is $(date +%M) minutes past $(date +%1%p).
The time is 26 minutes past 11AM.


No comments:

Post a Comment