0

Is there any way to achieve below.. I am stuck to perform my task.

I have written shell script which gives me correct output only when the script is getting executed on same month

below is the command which i am using in script

cd $(find /home/application/Files/output/$(date +%Y)/$(date +%m) -type d|sort -r |head -1)

Below is the directory format which is actually present in system

 /home/application/Files/output/2018/01/30

Now the issue is : when i execute my script it gives me below error

 find: `/home/application/Files/output/2018/02': No such file or directory

It's looking for ../2018/02 directory which is not yet created, so i need to perform my task on /home/application/Files/output/2018/01/30.

6
  • If date(1) is from GNU, look at: man date | m +/--date=STRING
    – D McKeon
    Commented Feb 3, 2018 at 5:55
  • am not sure how should i use your recommendation in below command cd $(find /home/application/Files/output/$(date +%Y)/$(date +%m) -type d|sort -r |head -1)
    – Satya
    Commented Feb 3, 2018 at 6:09
  • I want to move in the latest modified directory which is available under this path using below command cd $(find /home/application/Files/output/$(date +%Y)/ latest folder under this path ..!!
    – Satya
    Commented Feb 3, 2018 at 6:12
  • date +%Y/%m ; date --date="next month" +%Y/%m
    – D McKeon
    Commented Feb 3, 2018 at 6:16
  • Thanks for quick response however I want to perform some task under the directory.. My file is present under below directory /home/application/Files/output/2018/01/30
    – Satya
    Commented Feb 3, 2018 at 6:43

3 Answers 3

1

Why search for a YYYY/MM/DD/ directory that might not exist yet?

Just search for and cd to the most recent YYYY/MM/DD/ directory:

cd "$(find /home/application/Files/output/ -type d \
       -regex '.*/[0-9][0-9][0-9][0-9]/[0-9][0-9]/[0-9][0-9]$' -print0 | 
     sort -rVz | head -z -n 1)"

Note: requires GNU find, sort, and head for the -print0, and -z NUL-delimiter options.

If you prefer PCRE style regular expressions, you could do this instead:

cd "$(find /home/application/Files/output/ -type d -print0 | 
        grep -z -P '/\d{4}/\d\d/\d\d$' | sort -rVz | head -z -n 1)"

Alternatively, if you have to use the current day's directory whether it is empty or not, make the directory first and then cd into it:

dir="/home/application/Files/output/$(date +%Y/%M/%d)"
mkdir -p "$dir"
cd "$dir"

mkdir's -p option creates the directory if it doesn't already exist. It also creates any required parent directories. It does not cause an error or complain if the directory already exists.

0

Using GNU date and bash (or ksh93), this picks out today's directory but backtracks one day at a time if that date's directory is not found, until it finds an existing directory (or max 30 days back in time):

for (( i = 0; i < 30; ++i )); do
    dir="/home/application/Files/output/$( date -d "now -$i days" +'%Y/%m/%d' )"
    [ -d "$dir" ] && break
done

if [ ! -d "$dir" ]; then
    echo 'Can not find recent output directory!' >&2
    exit 1
fi

printf 'Using output directory "%s"\n' "$dir"
cd "$dir"
0

If the directories are always created the same day, or very close to the same date, they were named after:

cd $(find /home/application/Files/output/$(date +%Y)/ -mindepth 1 -maxdepth 1 \
  -mtime -2 -type d |sort -r |head -1)

I didn't test it. Maybe min/maxdepth needs to be 2. :)

This solutions requires the date of creation to be maximum 2 days old. Maybe you need to adjust that too.

Without newline:

 cd $(find somepath/$(date +%Y)/ -mindepth 1 -maxdepth 1 -mtime -2 -type d |sort -r |head -1)
7
  • I used below command but gave this error.. cd $(find /home/application/Files/output/$(date +%Y)/ -mtime -2 \ -type d -mindepth 1 -maxdepth 1 |sort -r |head -1) find: paths must precede expression: -type
    – Satya
    Commented Feb 3, 2018 at 7:07
  • Sorry, my fault, wrong parameter order. Corrected. Commented Feb 3, 2018 at 7:18
  • Used your corrected command, however again got below error find: Expected a positive decimal integer argument to -maxdepth, but got 1 -mtime'`
    – Satya
    Commented Feb 3, 2018 at 7:47
  • You need at least one blank between newline and -mtime, else 1-mtime will be read. Of course, a blank before the linebreak can't be wrong. :) Commented Feb 3, 2018 at 7:56
  • IMy question might be silly.. I am new to linux so asking my concern... I got below message find: Expected a positive decimal integer argument to -maxdepth, but got 1 ' `
    – Satya
    Commented Feb 3, 2018 at 8:26

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .