Linux Beginner’s Guide Part 3

Working With Files

Creating Files

The touch command is used to create files of any type, for example, you can create test.txt, test.jpg, test.doc but only specialized software can read and create content in these file types.

touch [OPTIONS] FILE

go to Documents directory: cd ~/Documents/

and create a few test files: touch test1.sh test2.sh test3.sh test4.sh

view the files created: ls

As you can see touch command can be used to create multiple files at once.

Editors like vi, vim and nano can be also used to create files, but we will learn later in this tutorial about them.

Creating Folders

The mkdir command can be used to create folders. The basic structure is:

mkdir [OPTIONS] folder

Let’s create a few folders in /Documents/ directory:

cd ~/Documents/

mkdir example example1 example2

ls

One nice feature of mkdir command is that you can create full paths using argument -p:

mkdir -p example3/example/inside/example

ls

cd example3

ls

cd example

Moving Files

The mv command is used to move a file from a place to another one. The mv command can have two arguments: source and destination. The source is the place where the file is located and the destination is the place where the file should be moved.

mv [SOURCE] [DESTINATION]

Switch to the Documents directory:

cd ~/Documents

Move test.sh file into Work directory, the file is the [SOURCE] and the directory is the [DESTINATION]:

mv test.sh Work

ls Work

mv command can be used also to move and in the same time rename the file.

mv test.sh Work test1.sh

ls

The mv command can be used to rename a file in the same directory:

mv test.sh test1.sh

With mv command multiple files can be moved at the same time.

mv test2.sh test3.sh test4.sh Work

ls

Copying Files

Making copies of a file is very useful and they can be created as a backup, as a template or can be transferred to different media types.

cp [OPTIONS] [SOURCE] [DESTINATION]

Use the command below to get to the Documents directory:

cd ~/Documents

The cp command is used to copy files and is similar to mv command, it requires at least these two arguments: [SOURCE] [DESTINATION]

Let’s say we have to copy /etc/passwd to the current directory:

cp /etc/passwd .

The second argument is the . (dot) which represents the current directory, acts like a shortcut.

Please note that permissions have impact on commands like cp, it is necessary to have execute permissions to access the directory and read permissions on the file being copied and also you must have write and execute permissions on the directory where the file will be copied.

dd command is a utility that copies files or entire partitions at a bit level.

dd [OPTIONS] [OPERAND]

dd command is very useful for:

  • deleting or cloning entire disks or partitions
  • copying raw data to external devices like memory sticks, CDs, DVDs, and so on
  • backup and restoring of Master boot record (MBR)
  • creating a file with a specific size filled with zeros, which can be used as swap file (virtual memory)

Let’s create, with the dd command, a file named /tmp/buffer with 100 blocks of zeros that are 1MB in size:

dd if=/dev/zero of=/tmp/buffer bs=1M count=100

Below you can find the description of a few arguments that dd uses:

if = input file = the input file to be read from

of = output file = the output file to be written

bs = block size (block size units: K, M, G, T)

count = the number of blocks to be read from the input file

To clone from hard drive to another bs or count must not be specified.

dd if=/dev/sda of=/dev/sdb

Removing Files

rm command is used to delete files and directories. Please keep in mind that if you delete the files they are not moved to “Trash”, they are removed permanently.

rm [OPTIONS] [FILE]

To remove a file use the following command: rm linux.txt

To remove a directory, use the following command: rm -r linux

Permissions have impact on commands like rm. To delete files within a directory, the user must have execute and write permissions on this directory.

Previous articleUsing help in Linux Terminal
Next articleLinux Beginner’s Guide Part 4
A.Sulthan, Ph.D.,
Author and Assistant Professor in Finance, Ardent fan of Arsenal FC. Always believe "The only good is knowledge and the only evil is ignorance - Socrates"
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments