Image

Knowledge base → Working with files and folders in Linux

[Virtual servers] [Shared hosting]
Date of publication: 09.10.2023

To work with files and folders, use the following commands:

1. File creation

touch /home/user/file-name.txt

2. Creating a folder

mkdir /home/user/Folder-name

2.1 Creating a folder recursively (with nested ones)

mkdir -p /home/user/Folder-name/Sub1/Sub2

3. Copying a file

cp /home/user/file-name.txt /home/user/copy-file.txt

3.1 Copying a directory with all attached files and folders

cp -R /home/user/Folder1 /home/user/Folder2

3.2 Copying a directory with all attached files and folders while maintaining rights and taking into account links

cp -a /home/user/Folder1 /home/user/Folder2

3.3 Copying all files with mp3 extension

cp /home/user/*.mp3 /home/user/Music-Folder

3.4 Alternative command to copy files recursively

rsync -a file.txt file_backup.txt

3.5 Alternative command to copy directories recursively

rsync -a /var/www/vhosts/domain1.tld/ /var/www/vhosts/domain2.tld/

4. Deleting a file

rm /home/user/file-name.txt

4.1 Deleting a directory with all attached files and folders

rm -r /home/user/Folder1

5. View a list of files in the current directory

ls

5.1 View a list of files in the current directory, taking into account rights and owner

ls -la

5.2 Calculating the size of the current directory

du -h

6. Editing files

6.1 Text editor vi

vi /home/user/some-text.txt

To edit a file, press the INS key, after editing is complete, press the Esc key, then press the colon as shown below:

  • :wq (save and exit)
  • :q! (exit without saving)

6.2 Text editor nano

Installation on debian/ubuntu:

apt install nano

Editing the file:

nano /home/user/some-text.txt

To exit, use Ctrl + X then Y - with saving, N - without.





No Comments Yet