Files and directories#
This page discusses tools that allow to manipulate with files in the Linux.
There are several tools that allows to show awailable files the following cell generates files tree that we’ll use as exmpale.
python3 << EOF
import string
import random
from pathlib import Path
def random_name(length: int = 8) -> str:
'Function to create a random file/directory name'
letters = string.ascii_lowercase
return ''.join(random.choice(letters) for i in range(length))
root_path = Path("/tmp")
current_path = root_path/"files_folders"
current_path.mkdir(exist_ok=True)
for i in range(50):
num = random.random()
if num < 0.2:
# Creating folder
current_path = current_path/random_name()
current_path.mkdir(exist_ok=True)
elif num < 0.6:
# Creating file
(current_path/random_name()).touch()
elif current_path.parent != root_path:
# Returning to the previous directory
current_path = current_path.parent
(current_path/"file.txt").touch()
EOF
List contents (ls)#
ls
command that allows to show files/directories in the folder.
The following python code creates some random files and folders - so we can see what they look like in the output of the ls
command.
Just by using the ls
command, we’ll get some files/folders listed in random order. But we can’t tell which of them are directories and which are files, their creation dates, the user who created them, and so on.
ls /tmp/files_folders
aitzzukt dpjoshmw jvraarww omvkdxef tpfjgpdx vmruldxu
bixjwusi ghabbnmc lbnfotix qpisovvr tykzpklw xmqzuinv
czfhzxld hwewtwoc nqfijnnz ticzbedc utttsuke xsgufbni
When using the ls -l
command, you receive additional information in a detailed, table-like format. The columns provide the following details:
Line that indicates whether the item is a directory, along with its permissions.
Number of links to the item.
The third and fourth columns are the user who owns the file and the Unix group of users to which the file belongs.
Size of item in bytes.
Time at which item was changed.
And last column is the name of the item.
ls /tmp/files_folders -l
total 16
-rw-rw-r-- 1 f-kobak-distance-desctop f-kobak-distance-desctop 0 Dec 29 23:39 aitzzukt
drwxrwxr-x 3 f-kobak-distance-desctop f-kobak-distance-desctop 4096 Dec 29 23:39 bixjwusi
-rw-rw-r-- 1 f-kobak-distance-desctop f-kobak-distance-desctop 0 Dec 29 23:39 czfhzxld
-rw-rw-r-- 1 f-kobak-distance-desctop f-kobak-distance-desctop 0 Dec 29 23:39 dpjoshmw
drwxrwxr-x 3 f-kobak-distance-desctop f-kobak-distance-desctop 4096 Dec 29 23:39 ghabbnmc
-rw-rw-r-- 1 f-kobak-distance-desctop f-kobak-distance-desctop 0 Dec 29 23:39 hwewtwoc
-rw-rw-r-- 1 f-kobak-distance-desctop f-kobak-distance-desctop 0 Dec 29 23:39 jvraarww
-rw-rw-r-- 1 f-kobak-distance-desctop f-kobak-distance-desctop 0 Dec 29 23:39 lbnfotix
-rw-rw-r-- 1 f-kobak-distance-desctop f-kobak-distance-desctop 0 Dec 29 23:39 nqfijnnz
-rw-rw-r-- 1 f-kobak-distance-desctop f-kobak-distance-desctop 0 Dec 29 23:39 omvkdxef
-rw-rw-r-- 1 f-kobak-distance-desctop f-kobak-distance-desctop 0 Dec 29 23:39 qpisovvr
-rw-rw-r-- 1 f-kobak-distance-desctop f-kobak-distance-desctop 0 Dec 29 23:39 ticzbedc
-rw-rw-r-- 1 f-kobak-distance-desctop f-kobak-distance-desctop 0 Dec 29 23:39 tpfjgpdx
drwxrwxr-x 2 f-kobak-distance-desctop f-kobak-distance-desctop 4096 Dec 29 23:39 tykzpklw
-rw-rw-r-- 1 f-kobak-distance-desctop f-kobak-distance-desctop 0 Dec 29 23:39 utttsuke
-rw-rw-r-- 1 f-kobak-distance-desctop f-kobak-distance-desctop 0 Dec 29 23:39 vmruldxu
drwxrwxr-x 4 f-kobak-distance-desctop f-kobak-distance-desctop 4096 Dec 29 23:39 xmqzuinv
-rw-rw-r-- 1 f-kobak-distance-desctop f-kobak-distance-desctop 0 Dec 29 23:39 xsgufbni
Make diretory (mkdir)#
To create a directory, use the mkdir
utility. For more information on this utility, see the corresponding section of the gnu manual.
Create parents#
By default, if you try to create a directory under another directory that doesn’t exist, you will an get error from the mkdir
utility. There is a -p
option which allows to suppress this behaviour - all necessary directories will be created automatically.
The following cell shows a typical message that you’ll get if you try to create a directory under directory that doesn’t exist.
rm -f -r /tmp/test
mkdir /tmp/test/my_dir | true
mkdir: cannot create directory ‘/tmp/test/my_dir’: No such file or directory
The same code but with the -p
option.
mkdir -p /tmp/test/my_dir
There are no any error messages. And the corresponding file tree is created, which is shown in the next cell.
tree /tmp/test
/tmp/test
└── my_dir
2 directories, 0 files
Note: You can use the -p
option to suppress the error associated with creating the directory twice. The following cell attempts to create /tmp/double_creation
twice, but the second attempt uses the -p
option.
mkdir /tmp/double_creation
mkdir -p /tmp/double_creation
rm -r /tmp/double_creation
Find#
Linux find
command allows you to search for files in the system. It have following syntax find <directory-to-search> <criteria> <action>
where:
<directory-to-search>
: Specifies the directory where you want to begin the search.<criteria>
: Defines the properties of the files you are searching for. This can include the file name, size, modification date, permissions, and more.<action>
: Specifies what to do with the found files. By default, it prints the path to the files, but it can also execute other commands on them.
While generating the file tree used as an example on this page, a random folder was created, containing a file named file.txt
. The following cell helps establish the specific path of the file using the find
utility.
find /tmp/files_folders -name file.txt
/tmp/files_folders/ghabbnmc/file.txt