Log In | Register | June 9, 2023


Linux - June 16, 2012

Create, delete, copy, and move files and directories.

Creating files and directories.

To create a empty file you can use a variety of tools. Touch for instance is an easy way to create a empty file.

touch new_file.txt

To create a directory there is a command called mkdir

mkdir /new_directory

The above command will create the folder /new_directory. Lets say we had a series of folders that we need created though. For instance, lets say we wanted to create /new_directory/sub_directory/sub_sub_directory and none of the parent directories actually exist. You can use the -p flag with mkdir and it will auto create each parent as needed.

mkdir -p /new_directory/sub_directory/sub_sub_directory

Deleting files and directories.

To delete a file you can use the rm command as such.

rm filename.txt

To remove a directory on the other hand, you have two options.

rmdir /directory_name

The above command will remove a directory.

rm -rf /directory_name

The above command will also remove the directory as long as the flags -rf are set. The -r is for recursive, which allows the rm utility to recursively go through each file and remove. The -f flag is for force, which assures you that you will not be prompted at anytime to confirm a deletion.

Copying files and directories.

For copying files and or directories you really only have 1 command you need to worry about. The cp command can be used to copy files and directories. For instance, lets say we need to copy file1.txt and store it as a backup in our /tmp folder.

cp -a file1.txt /tmp

You’ll notice that in the command above I also used a -a flag. This flag preserves the attributes such as mode, ownership, timestamps, as well as contexts for the file. The -a flag is not required, but it’s good practice.

You could also copy all of the contents from a directory recursively. For instance, lets say we want to copy all of the files in my home directory to the /tmp folder.

cp -aR ~/ /tmp

The above command will copy all files that are not hidden to the /tmp directory from my home directory. If we wanted to include all hidden files as well, we would just need to add the period after the ~/ portion. For instance:

cp -aR ~/. /tmp

The above command will copy all files, both hidden and regular files, to the /tmp directory while saving the file attributes along with the new files.

Moving/renaming files and directories.

In order to move a file from one place to another or even rename a file/directory you’d use the mv utility.

Lets say we wanted to move the file test1.txt to the /tmp directory.

mv test1.txt /tmp

The above command unlike the cp command will move the file completely to the /tmp directory. You can also rename the file using the mv utility as such.

mv test1.txt test2.txt

Running the above command renames the file from test1.txt to test2.txt.

The same applies to directories. You can use the mv on an entire directory to either rename it or move it as such.

mv /home/user1 /home/user2

The above command will rename the directory user1 to user2.

Post By: | FavoriteLoadingAdd to favorites


Tuesday, July 21, 2015

Hi Chinmaya,First thanks for rdeniag my post.Well, the one you specified is a “replacing” way in vi, can be done from outside using sed:sed ‘s/orgText/NewTest/’ fileBut if you read the post properly it tells about substitute based on character position range :-)

Leave a Comment

Need Help? Ask a Question

Ask anything you want from how to questions to debug. We're here to help.

You Must Be Logged In To Post A Question.

Log In or Register