Files in Linux
File types in Linux
In
Linux
everything is a file and tty terminals are no exception.
Run
ls -l $(tty)
Or a similar command
ls -l /dev/pts/1
crw--w----. 1 andrei tty 136, 1 Nov 19 12:43 /dev/pts/1
The first character is the file type.
There are seven file types available: regular files, directories, character devices, blocks, sockets, named pipes, symbolic links
- : regular file
d : directory
c : character device file
b : block device file
s : local socket file
p : named pipe
l : symbolic link
Type c means character. That is, this file can receive and display characters
If you run
ls -ld /etc
drwxr-xr-x. 118 root root 8192 Nov 22 00:10 /etc
You can see that the file type is d. That is directory
To view disks/blocks run
lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sda 8:0 0 8G 0 disk ├─sda1 8:1 0 1G 0 part /boot └─sda2 8:2 0 7G 0 part ├─centos-root 253:0 0 6.2G 0 lvm / └─centos-swap 253:1 0 820M 0 lvm [SWAP] sr0 11:0 1 57.7M 0 rom /run/media/andrei/VBox_GAs_6.1.10
If you run
ls -l /dev/sda*
brw-rw----. 1 root disk 8, 0 Nov 22 00:10 /dev/sda brw-rw----. 1 root disk 8, 1 Nov 22 00:10 /dev/sda1 brw-rw----. 1 root disk 8, 2 Nov 22 00:10 /dev/sda2
You can make sure that the file type is b. That is block
? means that there must be one arbitrary character.
ls -l /dev/sda?
brw-rw----. 1 root disk 8, 1 Nov 22 00:10 /dev/sda1 brw-rw----. 1 root disk 8, 2 Nov 22 00:10 /dev/sda2
[12] means that there must be either the character 1 or the character 2
ls -l /dev/sda[12]
brw-rw----. 1 root disk 8, 1 Nov 22 00:10 /dev/sda1 brw-rw----. 1 root disk 8, 2 Nov 22 00:10 /dev/sda2
When you want to find out the system version you can run
ls -l /etc/system-release
lrwxrwxrwx. 1 root root Nov 22 00:10 /etc/system-release -> centos-release
The file type l means link that is, this is not a real file, but a link
Run
ls -l /etc/system-release /etc/centos-release /etc/redhat-release
-rw-r--r--. 1 root root 37 Oct 23 17:53 /etc/centos-release lrwxrwxrwx. 1 root root Nov 22 00:10 /etc/redhat-release -> centos-release lrwxrwxrwx. 1 root root Nov 22 00:10 /etc/system-release -> centos-release
/etc/centos-release is a real (regular) file and the rest are links to it.
If you were using redhat then the real one would be redhat-release
Another way to get information about the version is to run lsb_release -d where -d means description
lsb_release -d
Description: CentOS Linux release 7.9.2009 (Core)
Find out the location of a file
You can find out where the lsb_release file is located with the command which lsb_release. To get additional information right away run
ls -lF $(which lsb_release)
-rwxr-xr-x. 1 root root 15929 Mar 27 2015 /usr/bin/lsb_release*
The -F option means showing the file types. That is why we were able to see * after lsb_release. * means that the file is executable.
To find out where the file came from you can use the rpm command with the options q (query) and f (file)
rpm -qf $(which lsb_release)
redhat-lsb-core-4.1-27.el7.centos.1.x86_64
To copy a file in interactive mode use the command cp with the option i (interactive)
interactive means that if a file with this name exists, you will be asked before it is overwritten
cp -i /etc/hosts .
cp -i /etc/hosts .
cp: overwrite ‘./hosts’?
To create a directory with a subdirectory inside it right away use the command mkdir with the option p (parent)
mkdir -p sites/heiheiru
ls -l sites
total 0
drwxrwxr-x. 2 andrei andrei 6 Nov 19 13:40 heiheiru
Create a file
touch newfile
You can do this with echo
echo "" newfile
echo -n newfile
Or with >, but I do not like this option, because on my Ubuntu I have to press CTRL + C
> newfile
Set a date
touch -d '15 July 1975' newfile
stat newfile
File: newfile Size: 0 Blocks: 0 IO Block: 4096 regular empty file Device: fd00h/64768d Inode: 1851125 Links: 1 Access: (0644/-rw-r--r--) Uid: (26167/avorotyn) Gid: ( 2999/sshusers) Access: 1975-07-15 00:00:00.000000000 +0200 Modify: 1975-07-15 00:00:00.000000000 +0200 Change: 2022-06-15 10:05:27.686885410 +0300 Birth: -
Create several files at once
If you need only a few files - you can simply list them separated by spaces
touch one two three
To create several files at once with names in sequence run
touch with {}
With this method you can create many files with one short command.
touch files/file{1..5}
ls -l files
-rw-rw-r--. 1 andrei andrei 0 Nov 22 00:10 file1 -rw-rw-r--. 1 andrei andrei 0 Nov 22 00:10 file2 -rw-rw-r--. 1 andrei andrei 0 Nov 22 00:10 file3 -rw-rw-r--. 1 andrei andrei 0 Nov 22 00:10 file4 -rw-rw-r--. 1 andrei andrei 0 Nov 22 00:10 file5
To copy the files directory to the sites directory with all its contents run
cp -R files sites
ls -l sites/files/
total 0 -rw-rw-r--. 1 andrei andrei 0 Nov 22 00:10 file1 -rw-rw-r--. 1 andrei andrei 0 Nov 22 00:10 file2 -rw-rw-r--. 1 andrei andrei 0 Nov 22 00:10 file3 -rw-rw-r--. 1 andrei andrei 0 Nov 22 00:10 file4 -rw-rw-r--. 1 andrei andrei 0 Nov 22 00:10 file5
The files directory was copied to the sites directory, that is, sites now has a subdirectory files.
If you have the utility installed tree you can visually study the nesting. If not - first run
sudo yum install tree
tree sites
sites └── files ├── file1 ├── file2 ├── file3 ├── file4 └── file5 1 directory, 5 files
You can create a link with the ln command, a symbolic link with ln -s
How to empty a file in linux
To delete the contents of a file use
: > filename
noclobber: Preventing file overwriting
With noclobber you can prevent overwriting of existing files.
echo "ABC" > sample
cat sample
ABC
echo "DEF" > sample
cat sample
DEF
set -o noclobber
set -o | grep noclobber
noclobber on
echo "GHI" > sample
zsh: file exists: sample
You can bypass the prohibition by adding | after >
echo "GHI" >| sample
cat sample
GHI
Disable the prohibition on overwriting
set +o noclobber
How to delete a file in linux
rm filename
File information
To get information about the properties of a file use the command stat
stat newfile
File: newfile Size: 0 Blocks: 0 IO Block: 4096 regular empty file Device: fd00h/64768d Inode: 1851125 Links: 1 Access: (0644/-rw-r--r--) Uid: (26167/avorotyn) Gid: ( 2999/sshusers) Access: 2022-06-15 09:53:28.402613374 +0300 Modify: 2022-06-15 09:53:28.402613374 +0300 Change: 2022-06-15 09:53:28.402613374 +0300 Birth: -
Find out the number of lines in a file linux
To count how many lines there are in the file today.log use
wc -l today.log
282608 today.log
Concatenating files
To concatenate the file one with the file two it is enough to run
cat one two
You can concatenate several files at once.
Let's look at three files
one
,
two
,
three
with the following content
one
Cool bookstore: Book24
two
Best hotels: Booking.com
three
Official Samsung Store: Samsung
Let's concatenate them and check the result - it will appear in the console as in the case of two files
cat one two three
Cool bookstore: Book24
Best hotels: Booking.com
Official Samsung Store: Samsung
Now let's merge the files into one new file big
cat one two three > big
vi big
Cool bookstore: Book24
Best hotels: Booking.com
Official Samsung Store: Samsung
To the new file big we can append the file one
cat big one
Cool bookstore: Book24
Best hotels: Booking.com
Official Samsung Store: Samsung
Cool bookstore: Book24
echo
The same actions can be performed with the command echo
Create a file of a certain size
Let's create the file 10M.txt with a size of 10 Mb
truncate -s 10M /usr/share/10M.txt
Find out the file encoding
Suppose you have just created the file one.txt
touch one.txt
To find out its encoding run
file -i one.txt
one.txt: inode/x-empty; charset=binary
Write something into the file and check the encoding again
echo heihei.ru < one.txt
file -i one.txt
one.txt: text/plain; charset=us-ascii
Make sure that the file -i command detected different encodings binary and us-ascii
You may have not us-ascii but another variant
Let's add Cyrillic characters to the file
echo добро << one.txt
file -i one.txt
one.txt: text/plain; charset=utf-8
Changing the encoding
Suppose you have a file
file.html
in the wrong encoding.
You can change the encoding of files in the following way:
iconv options -f from-encoding -t to-encoding inputfile(s) -o outputfile
Example:
cat file.html | iconv -f cp1251 -t utf-8 > new.html
In one direction. Then we open, edit, close.
cat new.html | iconv -f utf-8 -t cp1251 > file.html
Convert a UTF-8 file to a SHIFT-JIS file
iconv -f UTF-8 -t SHIFT-JIS utf-8-file > shift-jis-file
file -i shift-jis-file
shift-jis-file: text/plain; charset=unknown-8bit
touch sample echo "Привет" > sample file -i sample
sample: text/plain; charset=utf-8
iconv -f UTF-8 -t Windows-1251 sample > windows-1251-file
file -i windows-1251-file
windows-1251-file: text/plain; charset=iso-8859-1
As an option you can use convmv
If you have problems with encoding on a site I advise you to read the article «Site in the wrong encoding»
Change the file owner
Suppose you have a file one whose owners are the user user and the group group
-rwx------. 1 user group 21012480 Dec 1 16:49 one
To change the owners to andrei and heihei run
chown andrei:heihei one
-rwx------. 1 andrei heihei 21012480 Dec 1 16:49 one
The sections about searching and comparing files are now in separate articles.
Trim mp3
ffmpeg -i original.mp3 -vn -acodec copy -ss 00:00:00 -to 00:30:00 part_01.mp3
Article author: Andrei Olegovich