Linux check file info

how to find the meta information of a file using BASH [closed]

How to find the meta-information of a file in BASH? And how to extract and print it separately using cut and grep commands?

I’ve Found it Its » ls -l » Information About A File.. Like File Name, Permission, File Owner, Date etc..

find . -name «filename.txt» -print | xargs ls -iSl Will give you all files named filename.txt in the current directory (the dot). Then xargs is used to let ls get the standard input from the piped command. Then -iSl sorts and give you size and date. 6710 -rw-rw-r— 1 userA GroupB 2346 Nov 26 00:55 ./somefolder/filename.txt

4 Answers 4

Instead of parsing the output of ls using cut / grep , you should just use stat which takes a -c argument to specify the output format.

anthony@Zia:~$ stat -c '%n : %A : %U : %s' afiedt.buf .XCompose afiedt.buf : -rw-r--r-- : anthony : 178 .XCompose : lrwxrwxrwx : anthony : 38 

You can change the output format however you’d like; check the stat(1) manpage for details.

I’m just guessing here, but have you tried the command file ? It will try to identify what kind of file it is.

Actually, My Output Should Be Like This: File Name: File Type: File Owner: File Permission: File Size: I’ve Used ls -l To Find The Meta-Info Of a File. I Need To Extract And Print It Like Above.

@meandyxtreme Have a look at the cut command that is very good at picking values from columns. And, please, stop with the «Capital Letter At The Start Of Every Word» thing. It makes it very hard to read.

Sorry about the capitalizing of every word. Look what i typed [ur10cs164@linux lab]$ ls -l lab2.txt -rw-rw-r-- 1 ur10cs164 ur10cs164 801 Jul 19 16:40 lab2.txt so the above line shows all the meta-info. so now how to extract them seperately. i’ve tried ‘cut’. Maybe the format i’ve typed may wrong. Lemme know the correct method to extract from it

Источник

How to Use the Linux stat command

The stat command is a useful utility that reports info about a certain file or filesystem. It comes pre-installed with almost all the Linux systems out there, so no need to worry about installation.

Let’s check out how to use stat to get all the important data you need for a specific file/filesystem.

Читайте также:  Astra linux vmware kernel panic

Why stat command

Sometimes, you may want to know some key details about a certain file/filesystem, for example, the file’s size, access permissions, inode number, time of last access/modification etc. Agreed, you can check out a lot of details about a file using ls. However, stat offers far more in-depth about the target file/filesystem.

When should you use stat? Whenever you need those extra information. For a quick comparison, let’s check out the file info of file1.txt. To better understand the output, learn more about the lh command.

Now, let’s check out what stat has to offer.

That’s a lot of information! You don’t need all of them in everyday life but in some specific situation, stat comes in super handy.

Linux Stat Usage: Location

Most of the Linux commands run from the /usr/bin directory.

Linux Stat Usage: Checking file/filesystem info

We’ve already seen this in action, right? The command structure goes like this.

To see the in-depth info of a file/filesystem, run this command. In this case, it’s my trusty file1.txt.

There are TONS of information about the specific file, right? All the information presented is labelled. Depending on your requirement, pick up the important one. From my experience, the most used ones are file permissions, inode and Uid and/or Gid.

Now, let’s check out an example with a filesystem. In this case, it’ll be the mount point of the filesystem. For example, this command will show the information of the root filesystem.

In Linux, (almost) every single thing is a file. Any filesystem is a file itself, so the output won’t be any different.

Linux Stat Usage: Terse form

When you’re running stat normally, all the information is printed in a human-readable structure. Want a short and simple version of the output? Add the “-t” argument.

The terse format is a special format that uses a predefined list of stat format specifiers.

Linux Stat Usage: Custom format

This is another interesting usage of the stat command where you can design the desired output structure of stat. To do this task, stat offers a LONG list of available format specifiers.

Here’s how the command looks like.

For example, the “%A” specifier returns the file/filesystem permissions in a human-readable format.

The “%U” specifier returns the owner of the file/filesystem.

To get the size of the file, use the “%s” format specifier.

For the inode number, use “%i”.

Now, let’s combine all of them together into a single command line. It will look like this.

There are tons of other format specifiers that stat support. All of them are listed in the man page of stats.

Now, as we’ve previously seen the “-t” or “–terse” argument, it’s a predefined value for the following arguments.

Читайте также:  Rtl8192cu kali linux driver

As for the “–terse –file-system” argument, the predefined value is as follows.

These outputs are very useful if you’re using stat in any sort of script, especially in bash scripts. New to bash script? Check out the beginner’s guide to bash scripting.

Printf vs format

The stat command supports –printf argument that functions basically the same manner as –format. However, the main difference between them is how the output is produced.

Let’s get it clear with an example. Here, I’m running stat with both arguments with the same format specifier.

As we can see, the –format argument adds a new line after the output ends. However, the –printf doesn’t. To make sure that there’s a new line after the output, you have to add “\n” at the end of the format specifier string.

In certain situations, you might be actually working with a link. However, by default, stat doesn’t differentiate between a link and a physical file. There’s a dedicated argument to get around this issue. Just pass the “-L” argument.

Final thoughts

The stat tool is a pretty straightforward one. All of its functions are managed by simple arguments and flags. Once you know which one to use, you can reap the most benefit out of it.

To learn further in-depth about the stat command, I highly recommend checking out the man page. It contains all the supported format specifiers and some additional arguments that you may find interesting.

Источник

This post and this website contains affiliate links. See my disclosure about affiliate links.

how to view all details or metadata of a file in linux command line

When viewing a particular file in Linux, you might want to see all the relevant file metadata associated with it. The file metadata details includes information regarding its size, permissions, creation date, access date, inode number, uid/gid, file type etc.

There are mainly two different commands that you can use for this purpose, ls and stat. Both will print out almost the same information but in different format.

ls Command

The most useful of the two commands is ls, (at least in my opinion) which lists the file details. Using some command line options you can print out all the details and metadata information of the particular file.

The various command line options above prints out various information as detailed below

l : This uses the long listing format while printing out. This is much more informative than the default format.
i : Prints out the inode number of the file
s : Prints the file size in blocks
a : Prints out all entries and does not ignore any files
n : Prints out the numeric user id and group id
h : Print the sizes in human readable format.

Читайте также:  Wsl windows subsystem linux

The above command will print out all relevant metadata information about the file, but usually is not very human readable in its format. As long as you know what information is printed in each column it should work just fine.

If you prefer a much more human readable format, then you can use the stat command instead.

stat Command

The basic stat command works without any command line arguments, other than the file name…

[root 17:03:19] ~ # stat world_bkp
File: ‘world_bkp’
Size: 2434 Blocks: 8 IO Block: 4096 regular file
Device: 804h/2052d Inode: 262149 Links: 1
Access: (0644/-rw-r—r—) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2014-02-13 05:06:57.883217273 -0600
Modify: 2014-02-13 05:06:57.893217273 -0600
Change: 2014-02-13 05:06:57.893217273 -0600
Birth: —

This prints out almost the same information as the ls command, but prints it out in a much more human readable format. There is also a basic description for each value in the format.

Both the ls and stat commands can be used for any file descriptor, which means both the file as well as directories. stat can be used on file systems as well.

The -f command line option specifies to print out the file system status instead of the file status. You can see the difference by running the command with and without the -f option.

If you are using another command such as find or locate to print out files, then you can pipe (|) the output of that command to either ls or stat to print out more meaningful information. A simple example is

bash$ locate world_bkp | xargs stat

The above command will print out the file details exactly as before, but is useful if you didn’t know the exact location of the world_bkp file. Another example of piping to ls command is

bash$ locate world_bkp | xargs ls -lisan

Again, as with most Linux commands, you can input multiple files in the command line to print the details of multiple files or use the pipe to output details of multiple files.

Image Files

There are in fact a couple of more commands that shows you specific information of a file depending on the type of the file. If you want to see what the file type is, then the file command can help you out.

Also in the case of image files, the identify command that is part of the imagemagick package is a very good option. The identify command will print out the image specific properties such as the format, colorspace, channel information etc.

bash$ identify -verbose myimage.jpg

Источник

Оцените статью
Adblock
detector