5 Useful Commands to Manage File Types and System Time in Linux


If you're a Linux user, you probably know that command line interface is a powerful tool for managing your system. In this article, we'll introduce you to five useful commands that will help you manage file types and system time on your Linux machine.

File

The first command we'll discuss is file command. This command is used to determine type of a file. To use it, simply type "file" followed by name of file you want to examine. Here's an example −

file myfile.txt

In this example, we're asking file command to tell us what type of file "myfile.txt" is. output might look something like this −

myfile.txt: ASCII text

This tells us that myfile.txt is a plain text file encoded in ASCII format. file command can identify a wide variety of file types, including binary executables, image files, and compressed archives.

ls

The next command we'll discuss is ls command. This command is used to list contents of a directory. By default, it simply lists names of files in directory, but it can be used with a number of options to provide more detailed information. Here's an example −

ls -l

In this example, we're asking ls command to provide a detailed listing of files in current directory. "-l" option tells ls to display file permissions, ownership, size, and modification time for each file. Here's some sample output −

-rw-r--r-- 1 user user 1024 Mar 23 10:23 myfile.txt
drwxr-xr-x 2 user user 4096 Mar 23 10:24 mydir

In this example, we can see that there are two files in current directory: "myfile.txt" and "mydir". "myfile.txt" file is a regular file owned by "user" user and group. It's 1024 bytes in size and was last modified on March 23 at 10:23 AM. "mydir" file is a directory owned by "user" user and group, and it contains files and directories of its own.

Date

The date command is used to display and set system date and time. Here's an example −

date

In this example, we're asking date command to display current system date and time. output might look something like this −

Tue Mar 23 10:30:42 EDT 2023

This tells us that current date and time on system is Tuesday, March 23, 2023 at 10:30:42 AM Eastern Daylight Time. date command can also be used to set system date and time. Here's an example −

date -s "2023-03-24 10:00:00"

In this example, we're asking date command to set system date and time to March 24, 2023 at 10:00:00 AM. Note that you'll need root privileges to set system time.

Touch

The touch command is used to create an empty file or update modification time of an existing file. Here's an example −

touch myfile.txt

In this example, we're asking touch command to update modification time of file "myfile.txt". If file doesn't exist, touch will create it. touch command can be useful in a variety of situations, such as when you need to create a placeholder file or update timestamp of a file to reflect when it was last modified.

Stat

The stat command is used to display detailed information about a file, including its size, permissions, and access times. Here's an example −

stat myfile.txt

In this example, we're asking stat command to display information about file "myfile.txt". output might look something like this −

File: myfile.txt
Size: 1024        Blocks: 8          IO Block: 4096   regular file
Device: fd00h/64768d	Inode: 524303      Links: 1
Access: (0644/-rw-r--r--)  Uid: ( 1000/   user)   Gid: ( 1000/   user)
Access: 2023-03-23 10:23:00.000000000 -0400
Modify: 2023-03-23 10:23:00.000000000 -0400
Change: 2023-03-23 10:23:00.000000000 -0400

In this example, we can see that file "myfile.txt" is a regular file that's 1024 bytes in size. It has read and write permissions for owner and read permissions for everyone else. file was last accessed and modified on March 23, 2023 at 10:23 AM, and its metadata was last changed at same time.

Filemtime

The filemtime command is used to display last modification time of a file in Unix timestamp format. This can be useful when you need to programmatically determine when a file was last modified. Here's an example −

filemtime myfile.txt

In this example, we're asking filemtime command to display last modification time of file "myfile.txt" in Unix timestamp format. output might look something like this −

1648122156

This tells us that file "myfile.txt" was last modified on March 23, 2023 at 10:22:36 AM in Unix timestamp format.

Chmod

The chmod command is used to change permissions of a file or directory. This can be useful when you need to give or revoke permissions to certain users or groups. Here's an example −

chmod u+w myfile.txt

In this example, we're asking chmod command to add write permission to owner (u) of file "myfile.txt". This will allow owner to modify file. "w" stands for write permission. Other permissions include read (r) and execute (x) permission.

Chown

The chown command is used to change ownership of a file or directory. This can be useful when you need to change owner or group of a file or directory. Here's an example −

chown user1:user1 myfile.txt

In this example, we're asking chown command to change ownership of file "myfile.txt" to user "user1" and group "user1". This will make "user1" new owner of file.

Cp

The cp command is used to copy files and directories from one location to another. This can be useful when you need to make a backup copy of a file or when you need to move a file to a different location. Here's an example −

cp myfile.txt /home/user1/myfiles/

In this example, we're asking cp command to copy file "myfile.txt" to directory "/home/user1/myfiles/". This will create a new copy of file in new location.

Mv

The mv command is used to move or rename files and directories. This can be useful when you need to rename a file or move a file to a different location. Here's an example −

mv myfile.txt /home/user1/myfiles/renamedfile.txt

In this example, we're asking mv command to rename file "myfile.txt" to "renamedfile.txt" and move it to directory "/home/user1/myfiles/". This will rename file and move it to new location.

Conclusion

In conclusion, five commands we discussed in this article are just a few examples of many powerful tools available on Linux command line. By using these commands, you can gain greater control over your system and more easily manage your files and system time. Whether you're a seasoned Linux user or just getting started, these commands are sure to come in handy in your day-to-day work.

Updated on: 11-Apr-2023

217 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements