For beginners who want to learn Linux, it can be very difficult to adapt to the command line or terminal. Since terminals are more useful than graphical user interface programs to help users control Linux systems, we must be accustomed to running commands in the terminal. So in order to effectively memorize different Linux commands, you should use the terminal every day and understand how to use commands with different options and parameters.
Managing file types and setting time in Linux
Check out our previous Linux Tips series article:
- 5 Interesting Linux command-line tricks
- 10 Useful Linux command-line tricks for beginners
In this article, we're going to look at 5 of the terminal's and file-and time-related tips and tricks.
File types in Linux
In Linux, everything is files, and your devices, directories, and ordinary files are considered files.
There are different types of files in a Linux system:
- Normal files: May contain commands, documents, audio files, videos, images, archived files, etc.
- Device files: The system is used to access your hardware components.
There are two device files that represent storage devices: block files, such as hard disks, which read data in blocks, character files, and read data by character.
- Hard links and soft links: used to access files anywhere on the Linux file system.
- Named Pipes and sockets: allows for interaction between different processes.
1. Determine file type with ' file ' command
You can use the file command to determine the types of files as follows. The following shows an example of how different file types are determined with the file command.
[email protected] ~/linux-tricks $ dirbackup master.zipcrossroads-stable.tar.gz Num.txtED Ward-maya-2011-2012-new-remix.mp3 Reggea.xspflinux-security-optimization-book.gif tmp-link[email protected] ~/linux-tricks $ file backup/backup/: directory [email protected] ~/linux-tricks $ file master.zip master.zip:Zip ar Chive data, at least v1.0 to extract[email protected] ~/linux-tricks $ file Crossroads-stable.tar.gzcrossroads-stable.tar.gz:gzip compressed data, from Unix, last Modified:tue Apr 5 15:15:20 2011 [email protected] ~/linux-tricks $ file linux-security-optimization-book.gif Linux-security-optimization-book.gif:gif image data, version 89a, x 259[email protected] ~/linux-tricks $ file E Dward-maya-2011-2012-new-remix.mp3 Edward-maya-2011-2012-new-remix.mp3:audio file with ID3 version 2.3.0, contains: MPEG ADTS, Layer III, V1, 192 kbps, 44.1 kHz, jntstereo[email protected] ~/linux-tricks $ file/dev/sda1/dev/sdA1:block Special [email protected] ~/linux-tricks $ file/dev/tty1/dev/tty1:character Special
2. Determine file types with ' ls ' and ' dir ' commands
Another way to determine the file type is to display a long list of results with the LS and dir commands.
Use Ls-l to determine the type of a file.
When you view the file permissions, the first character displays the file type, and the other characters display the file permissions.
[Email protected] ~/linux-tricks $ ls-ltotal 6908drwxr-xr-x 2 tecmint tecmint 4096 Sep 9 11:46 backup-rw-r--r--1 Tecmint tecmint 1075620 Sep 9 11:47 crossroads-stable.tar.gz-rwxr-----1 tecmint tecmint 5916085 Sep 9 11:49 EDWA rd-maya-2011-2012-new-remix.mp3-rw-r--r--1 tecmint tecmint 42122 Sep 9 11:49 linux-security-optimization-book.gif-rw-r--r--1 tecmint tecmint 17627 Sep 9 11:46 master.zip-rw-r--r--1 Tecmint tecmint 5 Sep 9 11:48 num.txt-rw-r--r--1 tecmint tecmint 0 Sep 9 11:46 reggea.xspf-rw-r--r--1 Tecmint tecmint 5 Sep 9 11:47 Tmp-link
Using Ls-l to determine block and character files
[Email protected] ~/linux-tricks $ ls-l/DEV/SDA1BRW-RW----1 root disk 8, 1 Sep 9 10:53/dev/sda1[email protected] ~ /linux-tricks $ ls-l/DEV/TTY1CRW-RW----1 root TTY 4, 1 Sep 9 10:54/dev/tty1
Use Dir-l to determine the type of a file.
[Email protected] ~/linux-tricks $ dir-ltotal 6908drwxr-xr-x 2 tecmint tecmint 4096 Sep 9 11:46 backup-rw-r--r-- 1 tecmint tecmint 1075620 Sep 9 11:47 crossroads-stable.tar.gz-rwxr-----1 tecmint tecmint 5916085 Sep 9 11:49 EDW ard-maya-2011-2012-new-remix.mp3-rw-r--r--1 tecmint tecmint 42122 Sep 9 11:49 linux-security-optimization-book.gif-rw-r--r--1 tecmint tecmint 17627 Sep 9 11:46 master.zip-rw-r--r--1 Tecmint tecmint 5 Sep 9 11:48 num.txt-rw-r--r--1 tecmint tecmint 0 Sep 9 11:46 reggea.xspf-rw-r--r--1 tecmint tecmint 5 Sep 9 11:47 Tmp-link
3. Count the number of files of the specified type
Let's take a look at the technique of using ls,grep and WC commands in a directory to count the number of file types specified. The interaction between commands is done through named pipes.
- A command that grep– a user to search based on a given pattern or regular expression.
- wc– commands that are used to count lines, words, and characters.
Count the number of ordinary files
In Linux, ordinary files are represented by symbols.
[Email protected] ~/linux-tricks $ ls-l | grep ^-| Wc-l7
Number of statistics directories
In Linux, the directory is represented by the symbol D.
[Email protected] ~/linux-tricks $ ls-l | grep ^d | Wc-l1
Statistics Symbolic links and number of hard links
In Linux, symbolic links and hard links are denoted by the symbol L.
[Email protected] ~/linux-tricks $ ls-l | grep ^l | Wc-l0
Number of statistics block files and character files
In Linux, block and character files are represented by symbols B and C.
[Email protected] ~/linux-tricks $ ls-l/dev | grep ^b | Wc-l37[email protected] ~/linux-tricks $ ls-l/dev | grep ^c | wc-l159
4. Finding files in a Linux system
Let's take a look at some of the commands for finding files in a Linux system, including the locate, find, Whatis, and which commands.
Find a file with the Locate command
In the output below, I want to locate the Samba server configuration file in the system
[Email protected] ~/linux-tricks $ locate samba.conf/usr/lib/tmpfiles.d/samba.conf/var/lib/dpkg/info/ Samba.conffiles
Find a file with the Find command
To learn how to use the Find command in Linux, you can read our following article, which lists more than 30 examples of FIND commands and how to use them.
Locating commands with the Whatis command
The Whatis command is typically used to locate a command, which is special because it gives information about a command, and it can also find the Help manual entries for configuration files and commands.
[Email protected] ~/linux-tricks $ whatis bashbash (1) -GNU bourne-again shell[email protected] ~/linux-tricks $ what is Findfind (1) -Search for files in a directory Hierarchy[email protected] ~/linux-tricks $ whatis lsls (1) -li St Directory Contents
Locating commands with the which command
The which command is used to locate commands in the file system.
[Email protected] ~/linux-tricks $ which mkdir/bin/mkdir[email protected] ~/linux-tricks $ which bash/bin/bash[email prot Ected] ~/linux-tricks $ which find/usr/bin/find[email protected] ~/linux-tricks $ $ which ls/bin/ls
5. Time to process Linux systems
In a networked environment, it's a good practice to keep your Linux system time accurate. There are many services in the Linux system that require the right time to work properly under networked conditions.
Let's take a look at the commands you can use to manage your machine's time. In Linux, there are two ways of managing time: System time and hardware time.
System time is managed by the system clock, hardware time is managed by hardware clock.
To see your system time, date, and time zone, use the date command as follows.
[Email protected] ~/linux-tricks $ datewed Sep 9 12:25:40 IST 2015
Set the system time with date-s or date-set= "STRING" as follows.
[Email protected] ~/linux-tricks $ sudo date-s "12:27:00" Wed Sep 9 12:27:00 IST 2015[email protected] ~/linux-tricks $ sudo date--set= "12:27:00" Wed Sep 9 12:27:00 IST 2015
You can also set the time and date as follows.
[Email protected] ~/linux-tricks $ sudo date 090912302015Wed Sep 9 12:30:00 IST 2015
Use the CAL command to view the current date from the calendar.
[Email protected] ~/linux-tricks $ cal September Su Mo Tu We Th Fr Sa 1 2 3 4 5 6
7 8 9-ten-A- 27 28 29 30--
Use the Hwclock command to view the hardware clock time.
[Email protected] ~/linux-tricks $ sudo hwclockwednesday September 06:02:58 PM IST -0.200081 seconds
To set the hardware clock time, use the hwclock–set–date= "STRING" command as follows.
[Email protected] ~/linux-tricks $ sudo hwclock--set--date= "09/09/2015 12:33:00" [email protected] ~/linux-tricks $ sudo Hwclockwednesday September 12:33:11 PM IST -0.891163 seconds
The system time is set by the hardware clock time at startup, and when the system shuts down, the hardware time is reset to the system time.
So when you look at the system time and hardware time, they are the same unless you change the system time. When your CMOS power is low, the hardware time may be incorrect.
You can also use the hardware clock to set the system time as follows.
$ sudo hwclock--hctosys
You can also use the system clock time to set the hardware clock time as follows.
$ sudo hwclock--SYSTOHC
To see how long your Linux system has been running, you can use the uptime command.
[Email protected] ~/linux-tricks $ uptime12:36:27 up 1:43, 2 users, load average:1.39, 1.34, 1.45[email prot Ected] ~/linux-tricks $ uptime-pup 1 hour, Minutes[email protected] ~/linux-tricks $ uptime-s2015-09-09 10:52:47
Summarize
Understanding the file types in Linux is a good attempt for beginners, and time management is also important, especially on servers that require reliable and efficient management of services. I hope this guide will be of some help to you. If you have any feedback, don't forget to write a review for us. Keep in touch with us.
5 useful commands for managing file types and system time in Linux