Linux Beginner’s Guide Part 3

4 minutes read

Linux_logo_errorbits.com


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 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 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 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 transfered 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 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 writen

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.

Facebook Twitter LinkedIn Telegram Whatsapp Pocket

Related Posts:

Many Linux commands deal with sensitive data like system hardware, passwords or operate under exceptional circumstances. Prevents regular users from executing these commands by mistakes and helps to protect data and system integrity. By logging in as root enab...
Microsoft loves Linux so much that its latest operating system is based on Linux. Azure Sphere OS is designed to run on embedded devices and IoT. Source: Microsoft’s Newest OS is Based on Linux – OMG! Ubuntu!
In terms of stability and security, Linux is ranked the most stable operating system and at the same time, it’s quite easy to maintain and support. One of the reasons why Linux web hosting has gained popularity is its easily accessible configuration files and ...
Ubuntu is an open-source operating system developed by the San microsystems. Linux allows users and in this case developers to make changes to the program without prior authorization from the creator. You can gather a deeper understanding of Ubuntu Linux from ...
The ifconfig command is used to view the current network configuration for ethernet cards and iwconfig displays the network configuration for wireless cards. Because this is the output from a Virtual Machine the ethernet is called enp0s3, on hardware machines ...
Installing Packages/Applications/Software The yum (apt-get or apt in Ubuntu) command is used to install new packages and to update/upgrade the operating system, it requires administrative access, so it must be used with sudo. yum [OPTIONS] [COMMAND] apt-get [O...