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...
To install Golang in Kali Linux, you can follow these steps:Open the terminal on your Kali Linux system. Download the latest stable version of Golang from the official website. You can use the wget command to download it directly from the terminal. For example...
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...
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 ...
grep command is a text filter that searches for the input and returns the text lines by a given pattern. In order to start the example, use the following command to get to the /Documents/ folder: cd ~/Documents
Linux Fedora, often referred to simply as Fedora, is a popular open-source Linux-based operating system that is known for its focus on innovation, community-driven development, and cutting-edge features. Fedora is a free and open-source operating system that i...