Linux - Creating and Deleting Files and Directories (cd, mkdir, rmdir, rm)
*Review - Changing Directories
To change your working (current) directory, use the cd (change dir) command,
$ cd cse220
$ pwd
/home/fredf/cse220
$ ls
assgn02
$ cd ..
$ pwd
/home/fredf
Note: the 'cd ..' command changes the directory to the parent directory. In linux, '..' refers to the parent directory, while '.' refers to the current directory and finally '~' refers to your home directory. As you progress, you will see these are very useful.
Deleting Files
To delete a file use the rm (remove) command,
$ rm a.out
$ ls
file1.c file3 src
$ rm file3
$ ls -al
-rwxrwxrwx 1 fredf fredf 36 Sep 23 7:53 .bash_profile
-rwxr-xr-x 1 fredf fredf 1098 Mar 7 2005 file1.c
drwxr-xr-x+ 1 fredf fredf 0 Jan 01 2005 src
Note: You can only delete files that you own or have write privilege.
Creating and Removing Directories
To create a new directory use the mkdir (make directory) command and to remove it use the rmdir (remove directory) command,
$ mkdir cse220
$ ls
a.out cse220 file1.c file3 src
$ rmdir cse220
$ ls
a.out file1.c file3 src
$ mkdir cse220
$ mkdir cse220/assgn02
$ ls
a.out cse220 file1.c file3 src
$ ls cse220 assgn02
Suppose we have created directories cse220 and cse220/assgn02 and that Cse220/assgn02 has the files file1.c, file2.c, file1.o, file2.o, and a.out in it.
$ cd assgn02
$ pwd
/home/fredf/cse220/assgn02
$ ls
a.out file1.c file1.o file2.c file2.o
$ cd .
$ pwd /home/fredf/cse220/assgn02
$ cd .. $ pwd /home/fredf/cse220
$ cd ..
$ pwd
/home/fredf
Before deleting a directory, it must be empty (Learn how to delete non-empty directories),
$ rmdir cse220
rmdir: `cse220': Directory not empty
$ cd cse220
$ rmdir assgn02
rmdir: `assgn02': Directory not empty
$ cd assgn02
$ rm file1.c
$ rm file2.c
$ rm file1.o
$ rm file2.o
$ rm a.out
$ ls
$ cd ..
$ rmdir assgn02
$ cd ..
$ rmdir cse220
$ ls a.out file1.c file3 src
The command cd by itself will always change you back to your home directory. Remember, the symbol ~ also always refers to your home directory as well.
$ cd /usr/local/bin
$ pwd /usr/local/bin
$ cd
$ pwd
/home/fredf
$ cd /usr/local/bin
$ pwd /usr/local/bin
$ cd ~
$ pwd /home/fredf
$ cd cse220
$ cd assgn02
$ pwd /home/fredf/cse220/assgn02
$ rm ~/file1.c
The last command would remove
the file /home/fredf/file1.c.
Remember, however, that you can only delete a file if you have write privilege.
Previous - Changing File Permissions with 'chmod' Command | Next - File Name Globbing with *, ?, [ ]

No comments:
Post a Comment