- Get Last Modified Date of File in Linux
- 7 Answers 7
- Get a list of all files in folder and sub-folder in a file
- 7 Answers 7
- Get most recent file in a directory on Linux
- 21 Answers 21
- How to Find Files in Linux Using the Command Line
- Find a File in Linux by Name or Extension
- Using Common find Commands and Syntax to Find a File in Linux
- Basic Examples
- Options and Optimization for find
- Find a File in Linux by Modification Time
- Use grep to Find a File in Linux Based on Content
- How to Find and Process a File in Linux
- How to Find and Delete a File in Linux
- More Information
Get Last Modified Date of File in Linux
I’m new to Linux. I’m using the command-line. I’m trying to view the last modified date of a file. How do I do that in Linux from the Command Line?
7 Answers 7
As mentioned by @edvinas.me, stat tells you various information about the file including the last modified date.
At first, I was confused with Modify and Change, just to clarify, stat output lists:
- Access shows the time of last data access (e.g. read).
- Modify shows the time of last data modification.
- Change shows the time the file status last changed.
Use stat command for that:
Another way that is more flexible is using date -r . From man date :
This has the advantage of allowing you to specify the output format, e.g.
ls -l should do the work.
Building off of @Adam Taylor ‘s comment in @phoops ‘s answer and @Sparhawk ‘s answer.
To specifically just get the date (using October 3, 2019 for examples because it was my last birthday, here’s my venmo if you feel led to bless me financially: @levi_uzodike)
- stat -c %y file | cut -d’ ‘ -f1 will give you 2019-10-03
- date +%F -r file will also give you 2019-10-03
- date +%D -r file will give you 10/03/19
- date +%x -r file will probably give either 10/03/2019 , or 10/03/19 if you’re in the U.S. and either 03/10/2019 , or 03/10/19 if you’re in the U.K., just to name a couple examples (of course there are more possibilities)
These date format options are, to my understanding, combinations of other format options. Here are some explanations from the man page:
%b locale’s abbreviated month name (e.g., Jan)
%B locale’s full month name (e.g., January)
.
%d day of month (e.g, 01)
%D date; same as %m/%d/%y
%e day of month, space padded; same as %_d
%F full date; same as %Y-%m-%d
.
%m month (01..12)
.
%x locale’s date representation (e.g., 12/31/99)
.
%y last two digits of year (00..99)
%Y year
.
By default, date pads numeric fields with zeroes.
The following optional flags may follow `%’:
— (hyphen) do not pad the field
_ (underscore) pad with spaces
0 (zero) pad with zeros
^ use upper case if possible
# use opposite case if possible
N.B.: These flags don’t work on the «combo formats» like %F , %D and %x . They are for the «singular field formats«.
Apparently this last flag (#) does not work as I’d expect (e.g., if date +%b gives Oct , date +%#b gives OCT as opposed to oCT ) I guess this would be useless, but I’d think a lower case option would be more useful. date +%#p does turn date +%p which might give PM or AM into pm or am , respectively. So I guess it’s not a ‘per-character’ case switch but sets the case of all the characters in the string to the opposite case of the majority of the characters? Also date +%P gives pm or am , but neither date +%^P nor date +%#P change its output. My guess for this case is that %P is just an alias for %#p , and it seems that whenever you add more than one flag, the behavior is undefined/unpredictable ( e.g., date +%0-e gives the same as date +%-e : 3 and date +%-0e gives the same as date +%0e : 03 , which makes you think that only the flag next to the letter works or that it goes left to right, but both date +%#^p and date +%^#p give pm or am , [depending on the time of course] ) unless there’s some hidden order of operations? Sorry for digressing.
Also, if you run the command locale -k LC_TIME | grep ^d_fmt , you can see the combo for the specific locale of your system (e.g., d_fmt=»%m/%d/%Y» ).
And you can make your own combo. For example,
- date +%^b\ %-e\ %Y -r file will give you OCT 3 2019
Источник
Get a list of all files in folder and sub-folder in a file
How do I get a list of all files in a folder, including all the files within all the subfolders and put the output in a file?
7 Answers 7
You can do this on command line, using the -R switch (recursive) and then piping the output to a file thus:
this will make a file called filename1 in the current directory, containing a full directory listing of the current directory and all of the sub-directories under it.
You can list directories other than the current one by specifying the full path eg:
will list everything in and under /var and put the results in a file in the current directory called filename2. This works on directories owned by another user including root as long as you have read access for the directories.
You can also list directories you don’t have access to such as /root with the use of the sudo command. eg:
Would list everything in /root, putting the results in a file called filename3 in the current directory. Since most Ubuntu systems have nothing in this directory filename3 will not contain anything, but it would work if it did.
Just use the find command with the directory name. For example to see the files and all files within folders in your home directory, use
Check the find manual manpage for the find command
Also check find GNU info page by using info find command in a terminal.
An alternative to recursive ls is the command line tool tree that comes with quite a lot of options to customize the format of the output diplayed. See the manpage for tree for all options.
will give you the same as tree using other characters for the lines.
to display hidden files too
to not display lines
- Go to the folder you want to get a content list from.
- Select the files you want in your list ( Ctrl + A if you want the entire folder).
- Copy the content with Ctrl + C .
- Open gedit and paste the content using Ctrl + V . It will be pasted as a list and you can then save the file.
This method will not include subfolder, content though.
You could also use the GUI counterpart to Takkat’s tree suggestion which is Baobab. It is used to view folders and subfolders, often for the purpose of analysing disk usage. You may have it installed already if you are using a GNOME desktop (it is often called disk usage analyser).
You can select a folder and also view all its subfolders, while also getting the sizes of the folders and their contents as the screenshot below shows. You just click the small down arrow to view a subfolder within a folder. It is very useful for gaining a quick insight into what you’ve got in your folders and can produce viewable lists, but at the present moment it cannot export them to file. It has been requested as a feature, however, at Launchpad. You can even use it to view the root filesystem if you use gksudo baobab .
(You can also get a list of files with their sizes by using ls -shR
Источник
Get most recent file in a directory on Linux
Looking for a command that will return the single most recent file in a directory.
Not seeing a limit parameter to ls .
21 Answers 21
Not very elegant, but it works.
-A list all files except . and ..
-r reverse order while sorting
-t sort by time, newest first
This command actually gives the latest modified file in the current working directory.
This is a recursive version (i.e. it finds the most recently updated file in a certain directory or any of its subdirectory)
Edit: use -f 2- instead of -f 2 as suggested by Kevin
A note about reliability:
Since the newline character is as valid as any in a file name, any solution that relies on lines like the head / tail based ones are flawed.
With GNU ls , another option is to use the —quoting-style=shell-always option and a bash array:
(add the -A option to ls if you also want to consider hidden files).
If you want to limit to regular files (disregard directories, fifos, devices, symlinks, sockets. ), you’d need to resort to GNU find .
With bash 4.4 or newer (for readarray -d ) and GNU coreutils 8.25 or newer (for cut -z ):
Best here would be to use zsh and its glob qualifiers instead of bash to avoid all this hassle:
Newest regular file in the current directory:
Including hidden ones:
Check file age after symlink resolution:
Also, with the completion system ( compinit and co) enabled, Ctrl+X m becomes a completer that expands to the newest file.
Would make you edit the newest file (you also get a chance to see which it before you press Return ).
For the second-newest file.
for the newest c file.
for the newest regular file (not directory, nor fifo/device. ), and so on.
Источник
How to Find Files in Linux Using the Command Line
When you have to find a file in Linux, it’s sometimes not as easy as finding a file in another operating system. This is especially true if you are running Linux without a graphical user interface and need to rely on the command line. This article covers the basics of how to find a file in Linux using the CLI. The find command in Linux is used to find a file (or files) by recursively filtering objects in the file system based on a simple conditional mechanism. You can use the find command to search for a file or directory on your file system. By using the -exec flag ( find -exec ), files can be found and immediately processed within the same command.
Find a File in Linux by Name or Extension
Use find from the command line to locate a specific file by name or extension. The following example searches for *.err files in the /home/username/ directory and all sub-directories:
Using Common find Commands and Syntax to Find a File in Linux
find expressions take the following form:
- The options attribute will control the find process’s behavior and optimization method.
- The starting/path attribute will define the top-level directory where find begins filtering.
- The expression attribute controls the tests that search the directory hierarchy to produce output.
Consider the following example command:
This command enables the maximum optimization level (-O3) and allows find to follow symbolic links ( -L ). find searches the entire directory tree beneath /var/www/ for files that end with .html .
Basic Examples
Command | Description |
---|---|
find . -name testfile.txt | Find a file called testfile.txt in current and sub-directories. |
find /home -name *.jpg | Find all .jpg files in the /home and sub-directories. |
find . -type f -empty | Find an empty file within the current directory. |
find /home -user exampleuser -mtime -7 -iname «.db» | Find all .db files (ignoring text case) modified in the last 7 days by a user named exampleuser. |
Options and Optimization for find
The default configuration for find will ignore symbolic links (shortcut files). If you want find to follow and return symbolic links, you can add the -L option to the command, as shown in the example above.
find optimizes its filtering strategy to increase performance. Three user-selectable optimization levels are specified as -O1 , -O2 , and -O3 . The -O1 optimization is the default and forces find to filter based on filename before running all other tests.
Optimization at the -O2 level prioritizes file name filters, as in -O1 , and then runs all file-type filtering before proceeding with other more resource-intensive conditions. Level -O3 optimization allows find to perform the most severe optimization and reorders all tests based on their relative expense and the likelihood of their success.
Command | Description |
---|---|
-O1 | (Default) filter based on file name first. |
-O2 | File name first, then file type. |
-O3 | Allow find to automatically re-order the search based on efficient use of resources and likelihood of success. |
-maxdepth X | Search current directory as well as all sub-directories X levels deep. |
-iname | Search without regard for text case. |
-not | Return only results that do not match the test case. |
-type f | Search for files. |
-type d | Search for directories. |
Find a File in Linux by Modification Time
The find command contains the ability to filter a directory hierarchy based on when the file was last modified:
The first command returns a list of all files in the entire file system that end with the characters conf and modified in the last seven days. The second command filters exampleuser user’s home directory for files with names that end with the characters conf and modified in the previous three days.
Use grep to Find a File in Linux Based on Content
The find command can only filter the directory hierarchy based on a file’s name and metadata. If you need to search based on the file’s content, use a tool like grep . Consider the following example:
This searches every object in the current directory hierarchy ( . ) that is a file ( -type f ) and then runs the command grep «example» for every file that satisfies the conditions. The files that match are printed on the screen ( -print ). The curly braces ( <> ) are a placeholder for the find match results. The <> are enclosed in single quotes ( ‘ ) to avoid handing grep a malformed file name. The -exec command is terminated with a semicolon ( ; ), which should be escaped ( \; ) to avoid interpretation by the shell.
How to Find and Process a File in Linux
The -exec option runs commands against every object that matches the find expression. Consider the following example:
This filters every object in the current hierarchy ( . ) for files named rc.conf and runs the chmod o+r command to modify the find results’ file permissions.
The commands run with the -exec are executed in the find process’s root directory. Use -execdir to perform the specified command in the directory where the match resides. This may alleviate security concerns and produce a more desirable performance for some operations.
The -exec or -execdir options run without further prompts. If you prefer to be prompted before action is taken, replace -exec with -ok or -execdir with -okdir .
How to Find and Delete a File in Linux
To delete the files that end up matching your search, you can add -delete at the end of the expression. Do this only when you are positive the results will only match the files you wish to delete.
In the following example, find locates all files in the hierarchy starting at the current directory and fully recursing into the directory tree. In this example, find will delete all files that end with the characters .err :
More Information
You may wish to consult the following resources for additional information on this topic. While these are provided in the hope that they will be useful, please note that we cannot vouch for the accuracy or timeliness of externally hosted materials.
This page was originally published on Monday, October 25, 2010.
Источник