Skip to main content

File system Management

Command-line File Management

File management involves creating, deleting, copying, and moving files. Additionally, directories can be created, deleted, copied, and moved to help organize files logically. When working at the command line, file management requires awareness of the current working directory to choose either absolute or relative path syntax as most efficient for the immediate task.
File Management Commands
Activity Single source (note) Multiple source (note)
Copy file cp file1 file2 cp file1 file2 file3 dir (4)
Move file mv file1 file2 (1) mv file1 file2 file3 dir (4)
Remove file rm file1 rm -f file1 file2 file3 (5)
Create directory mkdir dir mkdir -p par1/par2/dir (6)
Copy directory cp -r dir1 dir2 (2) cp -r dir1 dir2 dir3 dir4 (4)
Move directory mv dir1 dir2 (3) mv dir1 dir2 dir3 dir4 (4)
Remove directory rm -r dir1 (2) rm -rf dir1 dir2 dir3 (5)
Note:
(1)The result is a rename.
(2)The "recursive" option is required to process a source directory.
(3)If dir2 exists, the result is a move. If dir2 doesn't exist, the result is a rename.
(4)The last argument must be a directory.
(5)Use caution with "force" option; you will not be prompted to confirm your action.
(6)Use caution with "create parent" option; typing errors are not caught.

Create Directories

The mkdir command creates one or more directories or subdirectories, generating errors if the file name already exists or when attempting to create a directory in a parent directory that doesn't exist. The -p parent option creates missing parent directories for the requested destination. Be cautious when using mkdir -p, since accidental spelling mistakes create unintended directories without generating error messages.
In the following example, a user attempts to use mkdir to create a subdirectory named Watched in the existing Videos directory, but mistypes the directory name.
[student@desktopX ~]$ mkdir Video/Watched
mkdir: cannot create directory `Video/Watched': No such file or directory
The mkdir failed because Videos was misspelled and the directory Video does not exist. If the user had used mkdir with the -p option, there would be no error and the user would end up with two directories, Videos and Video, and the Watched subdirectory would be created in the wrong place.
[student@desktopX ~]$ mkdir Videos/Watched
[student@desktopX ~]$ cd Documents
[student@desktopX Documents]$ mkdir ProjectX ProjectY
[student@desktopX Documents]$ mkdir -p Thesis/Chapter1 Thesis/Chapter2 Thesis/Chapter3
[student@desktopX Documents]$ cd
[student@desktopX ~]$ ls -R Videos Documents
Documents:
ProjectX  ProjectY  Thesis  thesis_chapter1.odf  thesis_chapter2.odf

Documents/ProjectX:

Documents/ProjectY:

Documents/Thesis:
Chapter1  Chapter2  Chapter3

Documents/Thesis/Chapter1:

Documents/Thesis/Chapter2:

Documents/Thesis/Chapter3:

Videos:
blockbuster1.ogg  blockbuster2.ogg  Watched

Videos/Watched:

[student@desktopX ~]$
The last mkdir created three ChapterN subdirectories with one command. The -p parent option created the missing parent directory Thesis.

Copy Files

The cp command copies one or more files to become new, independent files. Syntax allows copying an existing file to a new file in the current or another directory, or copying multiple files into another directory. 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.
[student@desktopX ~]$ cd Videos
[student@desktopX Videos]$ cp blockbuster1.ogg blockbuster3.ogg
[student@desktopX Videos]$ ls -l
total 0
-rw-rw-r--. 1 student student    0 Feb  8 16:23 blockbuster1.ogg
-rw-rw-r--. 1 student student    0 Feb  8 16:24 blockbuster2.ogg
-rw-rw-r--. 1 student student    0 Feb  8 19:02 blockbuster3.ogg
drwxrwxr-x. 2 student student 4096 Feb  8 23:35 Watched
[student@desktopX Videos]$
When copying multiple files with one command, the last argument must be a directory. Copied files retain their original names in the new directory. Conflicting file names that exist at a destination may be overwritten. To protect users from accidentally overwriting directories with contents, multiple file cp commands ignore directories specified as a source. Copying non-empty directories, with contents, requires the -r recursive option.
[student@desktopX Videos]$ cd ../Documents
[student@desktopX Documents]$ cp thesis_chapter1.odf thesis_chapter2.odf Thesis ProjectX
cp: omitting directory `Thesis'
[student@desktopX Documents]$ cp -r Thesis ProjectX
[student@desktopX Documents]$ cp thesis_chapter2.odf Thesis/Chapter2/
[student@desktopX Documents]$ ls -R
.:
ProjectX  ProjectY  Thesis  thesis_chapter1.odf  thesis_chapter2.odf

./ProjectX:
Thesis  thesis_chapter1.odf  thesis_chapter2.odf

./ProjectX/Thesis:

./ProjectY:

./Thesis:
Chapter1  Chapter2  Chapter3

./Thesis/Chapter1:

./Thesis/Chapter2:
thesis_chapter2.odf

./Thesis/Chapter3:
[student@desktopX Documents]$
In the first cp command, Thesis failed to copy, but thesis_chapter1.odf and thesis_chapter2.odf succeeded. Using the -r recursive option, copying Thesis succeeded.

Move Files

The mv command renames files in the same directory, or relocates files to a new directory. File contents remain unchanged. Files moved to a different file system require creating a new file by copying the source file, then deleting the source file. Although normally transparent to the user, large files may take noticeably longer to move.
[student@desktopX Videos]$ cd ../Documents
[student@desktopX Documents]$ ls -l
total 0
-rw-rw-r--. 1 student student    0 Feb  8 16:24 thesis_chapter1.odf
-rw-rw-r--. 1 student student    0 Feb  8 16:24 thesis_chapter2.odf
[student@desktopX Documents]$ mv thesis_chapter2.odf thesis_chapter2_reviewed.odf
[student@desktopX Documents]$ mv thesis_chapter1.odf Thesis/Chapter1
[student@desktopX Documents]$ ls -lR
.:
total 16
drwxrwxr-x. 2 student student 4096 Feb 11 11:58 ProjectX
drwxrwxr-x. 2 student student 4096 Feb 11 11:55 ProjectY
drwxrwxr-x. 5 student student 4096 Feb 11 11:56 Thesis
-rw-rw-r--. 1 student student    0 Feb 11 11:54 thesis_chapter2_reviewed.odf

./ProjectX:
total 0
-rw-rw-r--. 1 student student 0 Feb 11 11:58 thesis_chapter1.odf
-rw-rw-r--. 1 student student 0 Feb 11 11:58 thesis_chapter2.odf

./ProjectX/Thesis:
total 0

./ProjectY:
total 0

./Thesis:
total 12
drwxrwxr-x. 2 student student 4096 Feb 11 11:59 Chapter1
drwxrwxr-x. 2 student student 4096 Feb 11 11:56 Chapter2
drwxrwxr-x. 2 student student 4096 Feb 11 11:56 Chapter3

./Thesis/Chapter1:
total 0
-rw-rw-r--. 1 student student 0 Feb 11 11:54 thesis_chapter1.odf

./Thesis/Chapter2:
total 0
-rw-rw-r--. 1 student student 0 Feb 11 11:54 thesis_chapter2.odf

./Thesis/Chapter3:
total 0
[student@desktopX Documents]$
The first mv command is an example of renaming a file. The second causes the file to be relocated to another directory.

Remove Files and Directories

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.
[student@desktopX Documents]$ pwd
/home/student/Documents
[student@desktopX Documents]$ rm thesis_chapter2_reviewed.odf
[student@desktopX Documents]$ rm Thesis/Chapter1
rm: cannot remove `Thesis/Chapter1': Is a directory
[student@desktopX Documents]$ rm -r Thesis/Chapter1
[student@desktopX Documents]$ ls -l Thesis
total 8
drwxrwxr-x. 2 student student 4096 Feb 11 12:47 Chapter2
drwxrwxr-x. 2 student student 4096 Feb 11 12:48 Chapter3
[student@desktopX Documents]$ rm -ri Thesis
rm: descend into directory `Thesis'? y
rm: descend into directory `Thesis/Chapter2'? y
rm: remove regular empty file `Thesis/Chapter2/thesis_chapter2.odf'? y
rm: remove directory `Thesis/Chapter2'? y
rm: remove directory `Thesis/Chapter3'? y
rm: remove directory `Thesis'? y
[student@desktopX Documents]$
After rm failed to delete the Chapter1 directory, the -r recursive option succeeded. The last rm command parsed into each subdirectory first, individually deleting contained files before removing each now-empty directory. Using -i will interactively prompt for each deletion. This is essentially the opposite of -f which will force the deletion without prompting the user.
The rmdir command deletes directories only if empty. Removed directories cannot be undeleted.
[student@desktopX Documents]$ pwd
/home/student/Documents
[student@desktopX Documents]$ rmdir  ProjectY
[student@desktopX Documents]$ rmdir  ProjectX
rmdir: failed to remove `ProjectX': Directory not empty
[student@desktopX Documents]$ rm -r ProjectX
[student@desktopX Documents]$ ls -lR
.:
total 0
[student@desktopX Documents]$
The rmdir command failed to delete non-empty ProjectX, but rm -r succeeded.

Guided Exercise: Command Line File Management

In this exercise, you will practice efficient techniques for creating and organizing files using directories, file copies, and links.
Outcomes
Successfully create, move, copy, and delete files using the command line.
Before You Begin

Start your Amazon EC2 instance and use ssh to log in as the user ec2-user.
Steps
  1. Make sure you're in the home directory for your current user by entering the cd command. The pwd command should report /home/ec2-user, and your shell prompt should show ~ as the current directory.
    [ec2-user@ip-192-0-2-1 ~]$ cd
    [ec2-user@ip-192-0-2-1 ~]$ pwd
    /home/ec2-user
  2. In the home directory for ec2-user, create sets of empty practice files to use for the remainder of this lab. In each set, replace X with the numbers 1 through 6.
    Create six "song" files with names of the form songX.mp3.
    Create six "snapshot" files with names of the form snapX.jpg.
    Create six "movie" files with names of the form filmX.avi.

    [ec2-user@ip-192-0-2-1 ~]$ touch song1.mp3 song2.mp3 song3.mp3 song4.mp3 song5.mp3 song6.mp3
    [ec2-user@ip-192-0-2-1 ~]$ touch snap1.jpg snap2.jpg snap3.jpg snap4.jpg snap5.jpg snap6.jpg
    [ec2-user@ip-192-0-2-1 ~]$ touch film1.avi film2.avi film3.avi film4.avi film5.avi film6.avi
    [ec2-user@ip-192-0-2-1 ~]$ ls -l
    ...output omitted...
  3. In the ec2-user home directory, create three subdirectories, Music, Pictures, and Videos.
    [ec2-user@ip-192-0-2-1 ~]$ mkdir Music
    [ec2-user@ip-192-0-2-1 ~]$ mkdir Pictures
    [ec2-user@ip-192-0-2-1 ~]$ mkdir Videos
  4. From the ec2-user home directory, move the song files into the Music subdirectory, the snapshot files into the Pictures subdirectory, and the movie files into the Videos subdirectory.
    Once you are finished, use the ls -l command to list the contents of each of the subdirectories in order to check your work.

    [ec2-user@ip-192-0-2-1 ~]$ mv song1.mp3 song2.mp3 song3.mp3 song4.mp3 song5.mp3 song6.mp3 Music
    [ec2-user@ip-192-0-2-1 ~]$ mv snap1.jpg snap2.jpg snap3.jpg snap4.jpg snap5.jpg snap6.jpg Pictures
    [ec2-user@ip-192-0-2-1 ~]$ mv film1.avi film2.avi film3.avi film4.avi film5.avi film6.avi Videos
    [ec2-user@ip-192-0-2-1 ~]$ ls -l Music Pictures Videos
    ...output omitted...
  5. In the ec2-user home directory, create three additional subdirectories to organize the files into projects. Call these directories friends, family, and work. Create all three with one command.
    Check your work with the ls -l command.

    [ec2-user@ip-192-0-2-1 ~]$ mkdir friends family work
    [ec2-user@ip-192-0-2-1 ~]$ ls -l
    rwxrwxr-x. 2 ec2-user ec2-user 6 Apr 17 17:51 family
    drwxrwxr-x. 2 ec2-user ec2-user 6 Apr 17 17:51 friends
    drwxrwxr-x. 2 ec2-user ec2-user 6 Apr 17 17:47 Music
    drwxrwxr-x. 2 ec2-user ec2-user 6 Apr 17 17:47 Pictures
    drwxrwxr-x. 2 ec2-user ec2-user 6 Apr 17 17:47 Videos
    drwxrwxr-x. 2 ec2-user ec2-user 6 Apr 17 17:51 work
    
  6. Next, copy some files from the Music, Pictures, and Videos subdirectories to the friends and family subdirectories.
    Copy files in the Music, Pictures, and Videos subdirectories containing numbers 1 and 2 to the friends folder.
    Copy files in the Music, Pictures, and Videos subdirectories containing numbers 3 and 4 to the family folder.
    Before each copy, the example also changes directory to the destination directory that files are to be copied to. Note that the example uses the special relative directory name . to refer to the shell prompt's current directory for the copy commands. You may find it quicker to enter commands if you remember that you can use Tab to complete file names quickly. This is not the only possible solution to this step.
    Use ls to check your work.

    [ec2-user@ip-192-0-2-1 ~]$ cd friends
    [ec2-user@ip-192-0-2-1 friends]$ cp ~/Music/song1.mp3 ~/Music/song2.mp3 ~/Pictures/snap1.jpg ~/Pictures/snap2.jpg ~/Videos/film1.avi ~/Videos/film2.avi .
    [ec2-user@ip-192-0-2-1 friends]$ ls
    film1.mp3  film2.mp3  snap1.mp3  snap2.mp3  song1.mp3  song2.mp3
    [ec2-user@ip-192-0-2-1 friends]$ cd ../family
    [ec2-user@ip-192-0-2-1 family]$ cp ~/Music/song3.mp3 ~/Music/song4.mp3 ~/Pictures/snap3.jpg ~/Pictures/snap4.jpg ~/Videos/film3.avi ~/Videos/film4.avi .
    [ec2-user@ip-192-0-2-1 family]$ ls
    film3.mp3  film4.mp3  snap3.mp3  snap4.mp3  song3.mp3  song4.mp3
  7. Now copy files in the Music, Pictures, and Videos subdirectories containing numbers 5 and 6 to the work subdirectory.
    [ec2-user@ip-192-0-2-1 family]$ cd ../work
    [ec2-user@ip-192-0-2-1 work]$ cp ~/Music/song5.mp3 ~/Music/song6.mp3 ~/Pictures/snap5.jpg ~/Pictures/snap6.jpg ~/Videos/film5.avi ~/Videos/film6.avi .
    [ec2-user@ip-192-0-2-1 work]$ ls
    film5.mp3  film6.mp3  snap5.mp3  snap6.mp3  song5.mp3  song6.mp3
  8. Finally, it's time to delete the friends, family, and work directories.
    Change to the ec2-user home directory. Attempt to delete both the family and friends directories with a single rmdir command.

    [ec2-user@ip-192-0-2-1 work]$ cd
    [ec2-user@ip-192-0-2-1 ~]$ rmdir family friends
    rmdir: failed to remove `family': Directory not empty
    rmdir: failed to remove `friends': Directory not empty
    

    Using the rmdir command should fail since both directories have files in them.
  9. Use the rm command with the -r (recursive) option to delete the family and friends folders and their copies of the files.

    [ec2-user@ip-192-0-2-1 ~]$ rm -r family friends
    [ec2-user@ip-192-0-2-1 ~]$ ls
    Music  Pictures  Videos  work
  10. Delete all the files in the work subdirectory, but do not delete the work directory itself.

    [ec2-user@ip-192-0-2-1 ~]$ cd work
    [ec2-user@ip-192-0-2-1 work]$ rm song5.mp3 song6.mp3 snap5.jpg snap6.jpg film5.avi film6.avi
    [ec2-user@ip-192-0-2-1 work]$ ls -l
    total 0
    [ec2-user@ip-192-0-2-1 work]$ 
  11. Finally, from the home directory, use the rmdir command to delete the work directory. The command should succeed now that work is empty.

    [ec2-user@ip-192-0-2-1 work]$ cd
    [ec2-user@ip-192-0-2-1 ~]$ rmdir work
    [ec2-user@ip-192-0-2-1 ~]$ ls
    Music  Pictures  Videos
  12. This concludes this exercise. Log out and stop your Amazon EC2 instance.

Comments

Popular posts from this blog

Special Permissions in linux

The setuid permission on an executable file means that the command will run as the user owning the file, not as the user that ran the command. One example is the passwd command: [student@desktopX ~]$ ls -l /usr/bin/passwd -rw s r-xr-x. 1 root root 35504 Jul 16 2010 /usr/bin/passwd In a long listing, you can spot the setuid permissions by a lowercase s where you would normally expect the x (owner execute permissions) to be. If the owner does not have execute permissions, this will be replaced by an uppercase S . The special permission setgid on a directory means that files created in the directory will inherit their group ownership from the directory, rather than inheriting it from the creating user. This is commonly used on group collaborative directories to automatically change a file from the default private group to the shared group, or if files in a directory should be

The Seven-Step Model of Migration

Irrespective of the migration approach adopted, the Seven-step Model of Cloud Migration creates a more rational point of view towards the migration process and offers the ability to imbibe several best practices throughout the journey Step 1: Assess Cloud migration assessments are conducted to understand the complexities in the migration process at the code, design and architectural levels. The investment and the recurring costs are also evaluated along with gauging the tools, test cases, functionalities and other features related to the configuration. Step 2: Isolate The applications to be migrated to the cloud from the internal data center are freed of dependencies pertaining to the environment and the existing system. This step cuts a clearer picture about the complexity of the migration process. Step 3: Map Most organisations hold a detailed mapping of their environment with all the systems and applications. This information can be used to distinguish between the

RequestsDependencyWarning: urllib3 (1.24.1) or chardet (3.0.4) doesn't match a supported version

import tweepy /usr/lib/python2.7/dist-packages/requests/__init__.py:80: RequestsDependencyWarning: urllib3 (1.24.1) or chardet (3.0.4) doesn't match a supported version!   RequestsDependencyWarning) Traceback (most recent call last):   File "<stdin>", line 1, in <module>   File "/usr/local/lib/python2.7/dist-packages/tweepy/__init__.py", line 14, in <module>     from tweepy.api import API   File "/usr/local/lib/python2.7/dist-packages/tweepy/api.py", line 12, in <module>     from tweepy.binder import bind_api   File "/usr/local/lib/python2.7/dist-packages/tweepy/binder.py", line 11, in <module>     import requests   File "/usr/lib/python2.7/dist-packages/requests/__init__.py", line 97, in <module>     from . import utils   File "/usr/lib/python2.7/dist-packages/requests/utils.py", line 26, in <module>     from ._internal_utils import to_native_string   File "/usr/lib/python2.

tag