The most recent files in directory with a specific format in each one
up vote
-1
down vote
favorite
I would like to have in a variable the most recent file with a specific format.
exemple : in /home/test, 5 files :
file_test_hadoop_20181130.csv (This is the last modified one for hadoop file)- file_test_hadoop_20181130.txt
file_test_hadoop_20181130.ini
file_test_hub_20181130.txt (This is the last modified one for txt file)
- file_test_hub_20181130.csv
- file_test_hub_20181130.ini
So the result that i want is the last modified one in each type :
HADOOP_NAME=file_test_hadoop_20181130.csv
HUB_NAME=file_test_hub_20181130.txt
So i started to do something like this :
HADOOP_NAME=$(ls -tr /home/test | tail -n 1 | find /home/test -maxdepth 1 -name "file_test_hadoop*" -printf '%fn')
HUB_NAME=$(ls -tr /home/test | tail -n 1 | find /home/test -maxdepth 1 -name "file_test_hub*" -printf '%fn')
But i get all files.
shell-script files find ls
add a comment |
up vote
-1
down vote
favorite
I would like to have in a variable the most recent file with a specific format.
exemple : in /home/test, 5 files :
file_test_hadoop_20181130.csv (This is the last modified one for hadoop file)- file_test_hadoop_20181130.txt
file_test_hadoop_20181130.ini
file_test_hub_20181130.txt (This is the last modified one for txt file)
- file_test_hub_20181130.csv
- file_test_hub_20181130.ini
So the result that i want is the last modified one in each type :
HADOOP_NAME=file_test_hadoop_20181130.csv
HUB_NAME=file_test_hub_20181130.txt
So i started to do something like this :
HADOOP_NAME=$(ls -tr /home/test | tail -n 1 | find /home/test -maxdepth 1 -name "file_test_hadoop*" -printf '%fn')
HUB_NAME=$(ls -tr /home/test | tail -n 1 | find /home/test -maxdepth 1 -name "file_test_hub*" -printf '%fn')
But i get all files.
shell-script files find ls
add a comment |
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
I would like to have in a variable the most recent file with a specific format.
exemple : in /home/test, 5 files :
file_test_hadoop_20181130.csv (This is the last modified one for hadoop file)- file_test_hadoop_20181130.txt
file_test_hadoop_20181130.ini
file_test_hub_20181130.txt (This is the last modified one for txt file)
- file_test_hub_20181130.csv
- file_test_hub_20181130.ini
So the result that i want is the last modified one in each type :
HADOOP_NAME=file_test_hadoop_20181130.csv
HUB_NAME=file_test_hub_20181130.txt
So i started to do something like this :
HADOOP_NAME=$(ls -tr /home/test | tail -n 1 | find /home/test -maxdepth 1 -name "file_test_hadoop*" -printf '%fn')
HUB_NAME=$(ls -tr /home/test | tail -n 1 | find /home/test -maxdepth 1 -name "file_test_hub*" -printf '%fn')
But i get all files.
shell-script files find ls
I would like to have in a variable the most recent file with a specific format.
exemple : in /home/test, 5 files :
file_test_hadoop_20181130.csv (This is the last modified one for hadoop file)- file_test_hadoop_20181130.txt
file_test_hadoop_20181130.ini
file_test_hub_20181130.txt (This is the last modified one for txt file)
- file_test_hub_20181130.csv
- file_test_hub_20181130.ini
So the result that i want is the last modified one in each type :
HADOOP_NAME=file_test_hadoop_20181130.csv
HUB_NAME=file_test_hub_20181130.txt
So i started to do something like this :
HADOOP_NAME=$(ls -tr /home/test | tail -n 1 | find /home/test -maxdepth 1 -name "file_test_hadoop*" -printf '%fn')
HUB_NAME=$(ls -tr /home/test | tail -n 1 | find /home/test -maxdepth 1 -name "file_test_hub*" -printf '%fn')
But i get all files.
shell-script files find ls
shell-script files find ls
edited Nov 30 at 12:34
Rui F Ribeiro
38.5k1479128
38.5k1479128
asked Nov 30 at 10:47
amine tabenyoussef
105
105
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
up vote
0
down vote
So you want to retrieve the most recently modified file with a specific naming pattern. The following commands should work for you:
[haxiel@testvm1 ~]$ ls -1t file_test_hadoop* | head -n1
file_test_hadoop_20181130.csv
[haxiel@testvm1 ~]$ ls -1t file_test_hub* | head -n1
file_test_hub_20181130.txt
add a comment |
up vote
0
down vote
Tried with below command and it worked fine
ls -ltrh /home/test/file_test_hadoop*| tail -n1
ls -ltrh /home/test/file_test_hub* | tail -n1
add a comment |
up vote
0
down vote
Instead of trying to parse ls or pipe ls to find & print, just use bash arrays, shell globbing, and the natural sorting order of YYYYMMDD:
cd /home/test
hadoop_files=(file_test_hadoop*)
HADOOP_NAME=${hadoop_files[-1]}
unset hadoop_files
hub_files=(file_test_hub*)
HUB_NAME=${hub_files[-1]}
unset hub_files
cd -
This populates temporary array variables with the list of filenames that match the various patterns. You'd want to add error-checking for the situations where there are no files at all, or files of an unexpected pattern (file_test_hadoop9, for example).
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
So you want to retrieve the most recently modified file with a specific naming pattern. The following commands should work for you:
[haxiel@testvm1 ~]$ ls -1t file_test_hadoop* | head -n1
file_test_hadoop_20181130.csv
[haxiel@testvm1 ~]$ ls -1t file_test_hub* | head -n1
file_test_hub_20181130.txt
add a comment |
up vote
0
down vote
So you want to retrieve the most recently modified file with a specific naming pattern. The following commands should work for you:
[haxiel@testvm1 ~]$ ls -1t file_test_hadoop* | head -n1
file_test_hadoop_20181130.csv
[haxiel@testvm1 ~]$ ls -1t file_test_hub* | head -n1
file_test_hub_20181130.txt
add a comment |
up vote
0
down vote
up vote
0
down vote
So you want to retrieve the most recently modified file with a specific naming pattern. The following commands should work for you:
[haxiel@testvm1 ~]$ ls -1t file_test_hadoop* | head -n1
file_test_hadoop_20181130.csv
[haxiel@testvm1 ~]$ ls -1t file_test_hub* | head -n1
file_test_hub_20181130.txt
So you want to retrieve the most recently modified file with a specific naming pattern. The following commands should work for you:
[haxiel@testvm1 ~]$ ls -1t file_test_hadoop* | head -n1
file_test_hadoop_20181130.csv
[haxiel@testvm1 ~]$ ls -1t file_test_hub* | head -n1
file_test_hub_20181130.txt
answered Nov 30 at 10:56
Haxiel
826310
826310
add a comment |
add a comment |
up vote
0
down vote
Tried with below command and it worked fine
ls -ltrh /home/test/file_test_hadoop*| tail -n1
ls -ltrh /home/test/file_test_hub* | tail -n1
add a comment |
up vote
0
down vote
Tried with below command and it worked fine
ls -ltrh /home/test/file_test_hadoop*| tail -n1
ls -ltrh /home/test/file_test_hub* | tail -n1
add a comment |
up vote
0
down vote
up vote
0
down vote
Tried with below command and it worked fine
ls -ltrh /home/test/file_test_hadoop*| tail -n1
ls -ltrh /home/test/file_test_hub* | tail -n1
Tried with below command and it worked fine
ls -ltrh /home/test/file_test_hadoop*| tail -n1
ls -ltrh /home/test/file_test_hub* | tail -n1
answered Nov 30 at 15:13
Praveen Kumar BS
1,162138
1,162138
add a comment |
add a comment |
up vote
0
down vote
Instead of trying to parse ls or pipe ls to find & print, just use bash arrays, shell globbing, and the natural sorting order of YYYYMMDD:
cd /home/test
hadoop_files=(file_test_hadoop*)
HADOOP_NAME=${hadoop_files[-1]}
unset hadoop_files
hub_files=(file_test_hub*)
HUB_NAME=${hub_files[-1]}
unset hub_files
cd -
This populates temporary array variables with the list of filenames that match the various patterns. You'd want to add error-checking for the situations where there are no files at all, or files of an unexpected pattern (file_test_hadoop9, for example).
add a comment |
up vote
0
down vote
Instead of trying to parse ls or pipe ls to find & print, just use bash arrays, shell globbing, and the natural sorting order of YYYYMMDD:
cd /home/test
hadoop_files=(file_test_hadoop*)
HADOOP_NAME=${hadoop_files[-1]}
unset hadoop_files
hub_files=(file_test_hub*)
HUB_NAME=${hub_files[-1]}
unset hub_files
cd -
This populates temporary array variables with the list of filenames that match the various patterns. You'd want to add error-checking for the situations where there are no files at all, or files of an unexpected pattern (file_test_hadoop9, for example).
add a comment |
up vote
0
down vote
up vote
0
down vote
Instead of trying to parse ls or pipe ls to find & print, just use bash arrays, shell globbing, and the natural sorting order of YYYYMMDD:
cd /home/test
hadoop_files=(file_test_hadoop*)
HADOOP_NAME=${hadoop_files[-1]}
unset hadoop_files
hub_files=(file_test_hub*)
HUB_NAME=${hub_files[-1]}
unset hub_files
cd -
This populates temporary array variables with the list of filenames that match the various patterns. You'd want to add error-checking for the situations where there are no files at all, or files of an unexpected pattern (file_test_hadoop9, for example).
Instead of trying to parse ls or pipe ls to find & print, just use bash arrays, shell globbing, and the natural sorting order of YYYYMMDD:
cd /home/test
hadoop_files=(file_test_hadoop*)
HADOOP_NAME=${hadoop_files[-1]}
unset hadoop_files
hub_files=(file_test_hub*)
HUB_NAME=${hub_files[-1]}
unset hub_files
cd -
This populates temporary array variables with the list of filenames that match the various patterns. You'd want to add error-checking for the situations where there are no files at all, or files of an unexpected pattern (file_test_hadoop9, for example).
answered Nov 30 at 18:21
Jeff Schaller
37.5k1052121
37.5k1052121
add a comment |
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f485116%2fthe-most-recent-files-in-directory-with-a-specific-format-in-each-one%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown