Bash to select oldest folder in directory and write to log


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash to select oldest folder in directory and write to log
# 1  
Old 11-22-2016
Bash to select oldest folder in directory and write to log

In the bash below the oldest folder in a directory is selected. If there are 3folders in the directory /home/cmccabe/Desktop/NGS/test and nothing is done to them (ie. no files deleted, renamed) then the bash correctly identifies f1 as the oldest. However, if something is done to the folder then the bash identifies f2 as the oldest. I am not sure why or how to prevent that from happening. Thank you Smilie.

folders in directory
Code:
f1
f2
f3

Bash

Code:
# oldest folder used analysis and version log created 
dir=/home/cmccabe/Desktop/NGS/test 
{ 
read -r -d $'\t' time && read -r -d '' filename
 } < <(find "$dir" -maxdepth 1 -mindepth 1 -printf '%T+\t%P\0' | sort -z ) 
printf "The oldest folder is $filename, created on $time and analysis done using v1.3 by $USER at $(date "+%D %r")\n" >> /home/cmccabe/Desktop/NGS/test/log 
echo "$filename"


Last edited by cmccabe; 11-22-2016 at 09:39 AM.. Reason: fixed format
# 2  
Old 11-22-2016
Hi,
Could you give output of command find "$dir" -maxdepth 1 -mindepth 1 -printf '%T+\t%P\0'that we could see date format ?
Regards.
This User Gave Thanks to disedorgue For This Post:
# 3  
Old 11-22-2016
find "$dir" -maxdepth 1 -mindepth 1 -printf '%T+\t%P\0'

Code:
2016-11-22+08:10:45.2369135580    32016-11-22+08:14:45.8369117830    12016-11-22+08:1

Thank you Smilie.
# 4  
Old 11-22-2016
FWIW - folder==directory

Directories are files of metadata - data about other files. So directories in many ways behave like regular files in terms of timestamps on the directory itself.

there is a filetime in epoch seconds for:
Code:
create  (not all UNIX filesystems track this, OSX may for example)
modify (inode)
access - latest read for example

    When the directory itself was first created (mkdir) -- set create (birth) date 
    When the directory was last opened and modified -- set modify date:
          an object in the directory was added
          an object in the directory was deleted
          note: opening an existing file and writing to it does not change the modify date 
            of the directory
change: set ownership, set access permissions (chmod, chown can do this)

So, the answer is: when you work with files in a directory some actions changethe modify date of that directory, some do not. You CANNOT prevent it.

You can use the touch command to "correct" a directory's modify time. If you are using a linux filesystem that supports create times use that. See your man page or --help for how to access the create (birth) times.

Last edited by jim mcnamara; 11-22-2016 at 12:12 PM..
This User Gave Thanks to jim mcnamara For This Post:
# 5  
Old 11-23-2016
Thank you both Smilie.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash to check directory and create missing folder from file

In the below bash I am trying to ensure that all folders (represented by $folders) in a given directory are created. In the file f1 the trimmed folder will be there somewhere (will be multiple trimmed folders). When that trimmed folder is found (represented by $S5) the the contents of $2 printed... (19 Replies)
Discussion started by: cmccabe
19 Replies

2. Shell Programming and Scripting

Delete oldest folder based on folder named as date

Hi, I have a script doing backup to synology server, the script create new folder each day with the date as being folder name i.e. 2018-07-30. Just before creating the new folder I want the script to find the oldest folder from the list and delete it including its content. for example... (3 Replies)
Discussion started by: humble_learner
3 Replies

3. Shell Programming and Scripting

Bash to add folder to exsisting folders in directory

I am trying to create subdirectories in each folder in /home/cmccabe/Desktop/NGS/test/*. When I use echo I can see each folder in the directory, but I can not seem to add the commented out portion in bold. These are the sub-directories and sub-folders I am trying to add to each folder in... (1 Reply)
Discussion started by: cmccabe
1 Replies

4. Shell Programming and Scripting

Bash to select oldest folder in directory automatically and log process

The `bash` below uses the oldest folder in the specified directory and logs it. The goes though an analysis process and creates a log. My problem is that if there are 3 folders in the directory folder1,folder2,folder3, the bash is using folder2 for the analysis eventhough folder1 is the oldest... (0 Replies)
Discussion started by: cmccabe
0 Replies

5. Shell Programming and Scripting

Bash to select and save file in new directory

I am trying to select a file in bash and save it to a directory. The below does run but no selected file is saved. Thank you :). bash # select file printf "please select a file to analyze with entered gene or genes \n" select file in $(cd... (4 Replies)
Discussion started by: cmccabe
4 Replies

6. Shell Programming and Scripting

Bash to select panel then specific file in directory

I am using bash to prompt a user for a choice using: where a "y" response opens a menu with available panels that can be used. while true; do read -p "Do you want to get coverage of a specific panel?" yn case $yn in * ) menu; break;; * ) exit;; * ) echo... (6 Replies)
Discussion started by: cmccabe
6 Replies

7. Shell Programming and Scripting

Need help to write a script for moving the log files to some other folder

Hi Experts, I want to write a script, based upon the following requirement 1) I am having 5 application $ cd logs $ ls -l drwxr-xr-x 2 natraj nat 5.0K Sep 20 10:25 one drwxr-xr-x 2 natraj nat 5.0K Sep 20 10:39 two drwxr-xr-x 2 natraj nat 1.5K Sep 20 10:58... (4 Replies)
Discussion started by: natraj005
4 Replies

8. Shell Programming and Scripting

Deleting the oldest file in a directory

Hey! I have found similar posts both here and on other sites regarding this, but I cannot seem to get my script to work. I want to delete the oldest file in a test directory if there are more than two files. My script is currently: #!/bin/bash MEPATH=/usr/local/bin/test FILECOUNT=`ls... (4 Replies)
Discussion started by: Immolation
4 Replies

9. UNIX and Linux Applications

Finding the oldest file in a directory without ls

I am trying to determine the oldest and most recent files in a huge directory. I am using an ls -tr statement outside my find statement. The directory is too big and I am getting an "arg list too long" error. Is there something I can put in my find statement that doesn't create a list to... (2 Replies)
Discussion started by: hiyofjord
2 Replies

10. UNIX for Dummies Questions & Answers

Oldest File In A Directory

I'm writing a script to find the oldest file in a directory. I know this can be done by using ls -rt | tail -1 but these are rather large directories and that can be somewhat slow since the script will be running constantly. Are there any other ways to do this that would be faster? I looked to... (2 Replies)
Discussion started by: bergerj3
2 Replies
Login or Register to Ask a Question