Search This Blog

Tuesday, February 15, 2011

create delete cut copy file(s)/folder(s) in linux





Hi These are the most basic command a system administrator should aware of.

In this article i will try to cover all necessary commands.

Create Folder:

(1)Below command will create a folder named temp in root directory.

#mkdir /root/temp

Delete Folder:

(1)Below command will delete a folder named temp from root directory(provided there are no file(s) under temp directory.

#rmdir /root/temp

If the above directory contains any file(s) then we will get following error.

# rmdir /root/temp
rmdir: /root/temp: Directory not empty

Create file:

(1)Below command will create file named sample.txt under /root directory.

#touch /root/sample.txt

Delete file

(1)Below command will delete file named sample.txt from /root directory

# rm /root/sample.txt
rm: remove regular empty file `/root/sample.txt'? y

(2)To delete file without asking for confirmation use -f option along with rm command.

#rm -f /root/sample.txt

Copy file

(1)Copy one file to another folder.

# mkdir /tmp/dir1
# mkdir /tmp/dir2
# touch /tmp/dir1/file1.txt
# touch /tmp/dir2/file2.txt

To copy file1.txt from dir1 to dir2 issue following command.

# cp /tmp/dir1/file1.txt /tmp/dir2/

Now check the content of /tmp/dir2

# ls -ltr /tmp/dir2
total 8
-rw-r--r-- 1 root root 0 Feb 16 21:36 file2.txt
-rw-r--r-- 1 root root 0 Feb 16 21:38 file1.txt

As you can see file1.txt one is copied to dir2.

(2) Copy more than one file to another folder.

Now we will copy file1.txt and file2.txt to /tmp

# cp /tmp/dir2/file1.txt /tmp/dir2/file2.txt /tmp

Now check the content of /tmp

# ls -ltr /tmp
-rw-r--r-- 1 root root 0 Feb 16 21:44 file2.txt
-rw-r--r-- 1 root root 0 Feb 16 21:44 file1.txt

Move a file

(1) Lets move /tmp/file1.txt to root directory.

Check the content of /tmp directory
# ls -ltr /tmp
-rw-r--r-- 1 root root 0 Feb 16 21:44 file2.txt
-rw-r--r-- 1 root root 0 Feb 16 21:44 file1.txt

Move the file1.txt to /
#mv /tmp/file1.txt /

validating
# ls -ltr /file1.txt
-rw-r--r-- 1 root root 0 Feb 16 21:44 /file1.txt

(2)Use mv to rename a file.

You can also use mv to rename a file .
for example.

mv /file1.txt /newfile1.txt

above command will rename file1.txt in root folder to newfile.txt .

validating.
# ls -ltr /file1.txt
ls: /file1.txt: No such file or directory
[root@TomcatServer tmp]# ls -ltr /newfile1.txt
-rw-r--r-- 1 root root 0 Feb 16 21:44 /newfile1.txt

Move a folder

(1) Move a folder having sub folders and files to another folder.

Consider we are having following file structure under /tmp directory

now move dir1 along with its sub directories and files to dir2




# mv /tmp/dir1 /tmp/dir2




The end result should look like this.





No comments:

Post a Comment