Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place! |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
|
09-21-2015, 11:22 AM
|
#1
|
LQ Newbie
Registered: Aug 2008
Posts: 20
Rep:
|
How to List the distinct file name ?
Hi,
Please help me how can I display the unique/distinct file name from ls in linux. Will appreciate if you could let me know the exact command in order to get the below mentioned result.
The distinct output should only list the starting name list only for example only one charlie, only one tango and only one alpha like below one :
[example@informatica-srv1 test]$
-rwxrwxrwx 1 etl etl 937 Sep 13 22:00 charlie13150913_111_222629.unl
-rwxrwxrwx 1 etl etl 186 Sep 11 01:44 tango20150911_117_112263.unl
-rwxrwxrwx 1 etl etl 186 Sep 21 01:44 alpha20150921_123_033924.unl
Above output is what i am looking for, Again I need only one charlie, tango, alpha from the below mentioned output
[example@informatica-srv1 test]$ ls -ltr
-rwxrwxrwx 1 etl etl 937 Sep 13 22:00 charlie13150913_111_222629.unl
-rwxrwxrwx 1 etl etl 1498 Sep 13 22:02 charlie13150913_131_349632.unl
-rwxrwxrwx 1 etl etl 1135 Sep 13 22:02 charlie13150913_113_135911.unl
-rwxrwxrwx 1 etl etl 1343 Sep 13 22:02 charlie13150913_113_135912.unl
-rwxrwxrwx 1 etl etl 2627 Sep 13 22:02 charlie13150913_138_976607.unl
-rwxrwxrwx 1 etl etl 1506 Sep 13 22:03 charlie13150913_111_330091.unl
-rwxrwxrwx 1 etl etl 1690 Sep 13 22:03 charlie13150913_111_222630.unl
-rwxrwxrwx 1 etl etl 2637 Sep 13 22:03 charlie13150913_117_111111.unl
-rwxrwxrwx 1 etl etl 3002 Sep 13 22:04 charlie13150913_119_133130.unl
-rwxrwxrwx 1 etl etl 2256 Sep 13 22:04 charlie13150913_131_349633.unl
-rwxrwxrwx 1 etl etl 2442 Sep 13 22:04 charlie13150913_113_279957.unl
-rwxrwxrwx 1 etl etl 1312 Sep 13 22:04 charlie13150913_111_330092.unl
-rwxrwxrwx 1 etl etl 2247 Sep 13 22:04 charlie13150913_139_336782.unl
-rwxrwxrwx 1 etl etl 1677 Sep 13 22:04 charlie13150913_119_133131.unl
-rwxrwxrwx 1 etl etl 2238 Sep 13 22:04 charlie13150913_138_976608.unl
-rwxrwxrwx 1 etl etl 1398 Sep 13 22:04 charlie13150913_122_053970.unl
-rwxrwxrwx 1 etl etl 1877 Sep 13 22:04 charlie13150913_111_330093.unl
-rwxrwxrwx 1 etl etl 2240 Sep 13 22:04 charlie13150913_134_075453.unl
-rwxrwxrwx 1 etl etl 2796 Sep 13 22:05 charlie13150913_112_940211.unl
-rwxrwxrwx 1 etl etl 2249 Sep 13 22:05 charlie13150913_112_940222.unl
-rwxrwxrwx 1 etl etl 1852 Sep 13 22:05 charlie13150913_116_174685.unl
-rwxrwxrwx 1 etl etl 1370 Sep 13 22:05 charlie13150913_111_330094.unl
-rwxrwxrwx 1 etl etl 1687 Sep 13 22:05 charlie13150913_111_330095.unl
-rwxrwxrwx 1 etl etl 937 Sep 13 22:05 charlie13150913_113_135913.unl
-rwxrwxrwx 1 etl etl 1489 Sep 13 22:05 charlie13150913_113_135914.unl
-rwxrwxrwx 1 etl etl 2445 Sep 13 22:05 charlie13150913_113_174897.unl
-rwxrwxrwx 1 etl etl 1114 Sep 13 22:05 charlie13150913_122_053971.unl
-rwxrwxrwx 1 etl etl 186 Sep 11 01:44 tango20150911_117_112263.unl
-rwxrwxrwx 1 etl etl 185 Sep 11 01:44 tango20150911_117_112264.unl
-rwxrwxrwx 1 etl etl 188 Sep 11 01:44 tango20150911_117_112265.unl
-rwxrwxrwx 1 etl etl 376 Sep 11 01:44 tango20150911_116_174807.unl
-rwxrwxrwx 1 etl etl 186 Sep 11 01:44 tango20150911_116_174808.unl
-rwxrwxrwx 1 etl etl 186 Sep 11 01:44 tango20150911_116_174809.unl
-rwxrwxrwx 1 etl etl 378 Sep 11 01:44 tango20150911_116_174813.unl
-rwxrwxrwx 1 etl etl 381 Sep 11 01:44 tango20150911_138_976706.unl
-rwxrwxrwx 1 etl etl 378 Sep 11 01:44 tango20150911_138_976707.unl
-rwxrwxrwx 1 etl etl 192 Sep 11 01:44 tango20150911_113_280089.unl
-rwxrwxrwx 1 etl etl 381 Sep 11 01:44 tango20150911_113_280090.unl
-rwxrwxrwx 1 etl etl 376 Sep 11 01:44 tango20150911_132_327415.unl
-rwxrwxrwx 1 etl etl 193 Sep 11 01:44 tango20150911_132_327416.unl
-rwxrwxrwx 1 etl etl 380 Sep 11 01:44 tango20150911_132_327417.unl
-rwxrwxrwx 1 etl etl 188 Sep 11 01:44 tango20150911_132_327418.unl
-rwxrwxrwx 1 etl etl 188 Sep 11 01:44 tango20150911_133_329284.unl
-rwxrwxrwx 1 etl etl 177 Sep 11 01:44 tango20150911_133_329285.unl
-rwxrwxrwx 1 etl etl 186 Sep 21 01:44 alpha20150921_123_033924.unl
-rwxrwxrwx 1 etl etl 186 Sep 21 01:44 alpha20150921_117_212263.unl
-rwxrwxrwx 1 etl etl 185 Sep 21 01:44 alpha20150921_117_212264.unl
-rwxrwxrwx 1 etl etl 188 Sep 21 01:44 alpha20150921_117_212265.unl
-rwxrwxrwx 1 etl etl 376 Sep 21 01:44 alpha20150921_116_174807.unl
-rwxrwxrwx 1 etl etl 186 Sep 21 01:44 alpha20150921_116_174808.unl
-rwxrwxrwx 1 etl etl 186 Sep 21 01:44 alpha20150921_116_174809.unl
-rwxrwxrwx 1 etl etl 378 Sep 21 01:44 alpha20150921_116_174810.unl
-rwxrwxrwx 1 etl etl 381 Sep 21 01:44 alpha20150921_108_976706.unl
-rwxrwxrwx 1 etl etl 378 Sep 21 01:44 alpha20150921_108_976707.unl
-rwxrwxrwx 1 etl etl 192 Sep 21 01:44 alpha20150921_113_280089.unl
-rwxrwxrwx 1 etl etl 381 Sep 21 01:44 alpha20150921_113_280090.unl
-rwxrwxrwx 1 etl etl 376 Sep 21 01:44 alpha20150921_102_327415.unl
-rwxrwxrwx 1 etl etl 193 Sep 21 01:44 alpha20150921_102_327416.unl
-rwxrwxrwx 1 etl etl 380 Sep 21 01:44 alpha20150921_102_327417.unl
-rwxrwxrwx 1 etl etl 188 Sep 21 01:44 alpha20150921_102_327418.unl
-rwxrwxrwx 1 etl etl 188 Sep 21 01:44 alpha20150921_103_329284.unl
-rwxrwxrwx 1 etl etl 177 Sep 21 01:44 alpha20150921_103_329285.unl
-rwxrwxrwx 1 etl etl 190 Sep 21 01:44 alpha20150921_118_242622.unl
Last edited by imujjwal; 09-21-2015 at 11:33 PM.
|
|
|
09-21-2015, 11:48 AM
|
#2
|
LQ Guru
Registered: Dec 2008
Location: Westgate-on-Sea, Kent, UK
Distribution: Debian Testing Amd64
Posts: 5,465
Rep: 
|
This sounds very much like a homework question. If you need some help why not show us what you have tried so far.
jdk
Last edited by jdkaye; 09-21-2015 at 11:51 AM.
|
|
1 members found this post helpful.
|
09-21-2015, 11:55 AM
|
#3
|
Moderator
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,955
|
Please do not cross post duplicate thread here, I've asked that the duplicate thread be closed and that this one be used to address your question.
Firstly:
- What have you tried on your own?
Have you tried a script?
Have you tried different forms of the ls command?
Have you tried date qualifiers for files, or different regular expressions to filter the output as you'd like it?
You've shown example only and there can be multiple interpretations of what one, singular file you wish to identify out of a directory of many files, and further, your directory may contain a much larger variety of files beyond that limited example, so please think about a better problem description.
And finally, realize that this does appear like a homework or assignment question and if so, it is not policy for LQ responders to provide assistance with those beyond helping you beyond a blocking point, and where you've shown some effort in the first place. So if you really are just experimenting and trying to figure stuff out, go back to the part where you should illustrate some of your efforts, and next offer some explanations on a better scoping of the problem, and also state why you are attempting this particular problem. Some of the questions I've asked above can at least provide you with possible directions in exploring a solution.
|
|
2 members found this post helpful.
|
09-21-2015, 12:11 PM
|
#4
|
Member
Registered: Aug 2013
Location: Sweden
Distribution: Debian, Arch, Red Hat, CentOS
Posts: 773
|
Well, I solved this with a small script (14 lines).
Given a directory that looks like this:
Code:
$ ls -l
totalt 0
-rw-r--r-- 1 HMW HMW 0 sep 21 18:43 alpha20150921_117_212263.unl
-rw-r--r-- 1 HMW HMW 0 sep 21 18:43 alpha20150921_123_033924.unl
-rw-r--r-- 1 HMW HMW 0 sep 21 18:43 charlie13150913_111_222629.unl
-rw-r--r-- 1 HMW HMW 0 sep 21 18:43 charlie13150913_131_349632.unl
-rw-r--r-- 1 HMW HMW 0 sep 21 18:43 tango20150911_117_112263.unl
-rw-r--r-- 1 HMW HMW 0 sep 21 18:43 tango20150911_117_112264.unl
My script produces this output:
Code:
$ ./ls_uniq.sh
-rw-r--r-- 1 HMW HMW 0 sep 21 18:43 alpha20150921_117_212263.unl
-rw-r--r-- 1 HMW HMW 0 sep 21 18:43 charlie13150913_111_222629.unl
-rw-r--r-- 1 HMW HMW 0 sep 21 18:43 tango20150911_117_112263.unl
You might be able to do this with a oneliner, but I am not sure how.
Edit: Please note that I will be more than happy to share my solution as soon as the OP is willing to show some of his/her own efforts.
Best regards,
HMW
Last edited by HMW; 09-21-2015 at 12:15 PM.
|
|
2 members found this post helpful.
|
09-21-2015, 01:06 PM
|
#5
|
LQ Guru
Registered: Sep 2009
Location: Perth
Distribution: Arch
Posts: 10,037
|
My one liner gets similar:
Code:
$ ls -l
total 0
-rw-r--r-- 1 grail users 0 22.09.2015 02:01 alpha20150921_117_212263.unl
-rw-r--r-- 1 grail users 0 22.09.2015 02:01 alpha20150921_123_033924.unl
-rw-r--r-- 1 grail users 0 22.09.2015 02:01 charlie13150913_111_222629.unl
-rw-r--r-- 1 grail users 0 22.09.2015 02:01 charlie13150913_131_349632.unl
-rw-r--r-- 1 grail users 0 22.09.2015 02:01 tango20150911_117_112263.unl
-rw-r--r-- 1 grail users 0 22.09.2015 02:01 tango20150911_117_112264.unl
$ ls -l | awk '<magic>'
total 0
-rw-r--r-- 1 grail users 0 22.09.2015 02:01 alpha20150921_117_212263.unl
-rw-r--r-- 1 grail users 0 22.09.2015 02:01 charlie13150913_111_222629.unl
-rw-r--r-- 1 grail users 0 22.09.2015 02:01 tango20150911_117_112263.unl
|
|
1 members found this post helpful.
|
09-21-2015, 01:21 PM
|
#6
|
Senior Member
Registered: Dec 2009
Location: New Jersey, USA
Distribution: Fedora, OpenSUSE, FreeBSD, OpenBSD, macOS (hack). Past: Debian, Arch, RedHat (pre-RHEL).
Posts: 1,335
|
I'm not doing homework either, done with that twenty years ago  but I got bored - so in a directory looking like this:
Code:
total 0
-rw------- 1 anthony anthony 0 Sep 21 14:18 alpha20150921_10325_7wzQsU.unl
-rw------- 1 anthony anthony 0 Sep 21 14:18 alpha20150921_10331_cA7TpG.unl
-rw------- 1 anthony anthony 0 Sep 21 14:18 alpha20150921_12135_9oQG3V.unl
-rw------- 1 anthony anthony 0 Sep 21 14:18 alpha20150921_14529_XrhYSJ.unl
-rw------- 1 anthony anthony 0 Sep 21 14:18 alpha20150921_14810_Ndu6J3.unl
-rw------- 1 anthony anthony 0 Sep 21 14:18 alpha20150921_16052_uJKCn9.unl
-rw------- 1 anthony anthony 0 Sep 21 14:18 alpha20150921_21120_Ge7ySU.unl
-rw------- 1 anthony anthony 0 Sep 21 14:18 alpha20150921_21779_Wd3qI0.unl
-rw------- 1 anthony anthony 0 Sep 21 14:18 alpha20150921_2646_Qb7aPy.unl
-rw------- 1 anthony anthony 0 Sep 21 14:18 alpha20150921_6418_v3MUx4.unl
-rw------- 1 anthony anthony 0 Sep 21 14:18 alpha20150921_7816_DJeBvq.unl
-rw------- 1 anthony anthony 0 Sep 21 14:18 charlie20150921_17713_4oUIbb.unl
-rw------- 1 anthony anthony 0 Sep 21 14:18 charlie20150921_20261_3uj2JL.unl
-rw------- 1 anthony anthony 0 Sep 21 14:18 charlie20150921_20316_fgWEik.unl
-rw------- 1 anthony anthony 0 Sep 21 14:18 charlie20150921_22347_fXxoiT.unl
-rw------- 1 anthony anthony 0 Sep 21 14:18 charlie20150921_24964_TpJSJa.unl
-rw------- 1 anthony anthony 0 Sep 21 14:18 charlie20150921_29040_hg41Hx.unl
-rw------- 1 anthony anthony 0 Sep 21 14:18 charlie20150921_30206_y2EQLN.unl
-rw------- 1 anthony anthony 0 Sep 21 14:18 charlie20150921_30671_NJvI4G.unl
-rw------- 1 anthony anthony 0 Sep 21 14:18 charlie20150921_31925_FGcN1U.unl
-rw------- 1 anthony anthony 0 Sep 21 14:18 charlie20150921_8281_RplZ9X.unl
-rw------- 1 anthony anthony 0 Sep 21 14:18 charlie20150921_9981_KgBx4q.unl
-rw------- 1 anthony anthony 0 Sep 21 14:18 tango20150921_10997_LK5bKL.unl
-rw------- 1 anthony anthony 0 Sep 21 14:18 tango20150921_12670_tgsCMs.unl
-rw------- 1 anthony anthony 0 Sep 21 14:18 tango20150921_14430_gQ2Yus.unl
-rw------- 1 anthony anthony 0 Sep 21 14:18 tango20150921_18753_9y9dxw.unl
-rw------- 1 anthony anthony 0 Sep 21 14:18 tango20150921_23467_xPE1XC.unl
-rw------- 1 anthony anthony 0 Sep 21 14:18 tango20150921_27802_EOy3uz.unl
-rw------- 1 anthony anthony 0 Sep 21 14:18 tango20150921_2799_2tUT04.unl
-rw------- 1 anthony anthony 0 Sep 21 14:18 tango20150921_279_VEvy7h.unl
-rw------- 1 anthony anthony 0 Sep 21 14:18 tango20150921_4870_ZYMk6Z.unl
-rw------- 1 anthony anthony 0 Sep 21 14:18 tango20150921_6156_w9l3uP.unl
-rw------- 1 anthony anthony 0 Sep 21 14:18 tango20150921_9364_cweOP7.unl
just for sh*** and giggles, my one liner produces:
Code:
anthony@serenity: ~/tmp/tmp_working $ ls | <binutils-fu>
alpha20150921_10325_7wzQsU.unl
charlie20150921_17713_4oUIbb.unl
tango20150921_10997_LK5bKL.unl
I can also have it produce the first file entry, any entry for each prefix, whatever...
ETA: I'm not using awk like grail. BTW grail I had mine first, but forgot to hit submit. ;P
Last edited by goumba; 09-21-2015 at 01:25 PM.
|
|
1 members found this post helpful.
|
09-21-2015, 01:25 PM
|
#7
|
Moderator
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,955
|
This is like that old game show "Name That Tune"
"I can fix that problem in 4 characters!"

|
|
|
09-21-2015, 01:31 PM
|
#8
|
Member
Registered: Aug 2013
Location: Sweden
Distribution: Debian, Arch, Red Hat, CentOS
Posts: 773
|
Quote:
Originally Posted by rtmistler
"I can fix that problem in 4 characters!"

|
^This... is why we are called geeks.
Although this particular geek gave up that contest a long time ago. My code might be longer, but as long as it is easy to read and to modify, I am content.
|
|
1 members found this post helpful.
|
09-21-2015, 11:36 PM
|
#9
|
LQ Newbie
Registered: Aug 2008
Posts: 20
Original Poster
Rep:
|
Quote:
Originally Posted by jdkaye
This sounds very much like a homework question. If you need some help why not show us what you have tried so far.
jdk
|
I tried following one but could not get the exact output, the one i have mentioned above
[example@informatica-srv1 ~]$ ls *.* | awk -F\. '{print $(NF);}' | sort | uniq
csv
txt
unl
|
|
|
09-21-2015, 11:40 PM
|
#10
|
LQ Newbie
Registered: Aug 2008
Posts: 20
Original Poster
Rep:
|
Quote:
Originally Posted by rtmistler
Please do not cross post duplicate thread here, I've asked that the duplicate thread be closed and that this one be used to address your question.
Firstly:
- What have you tried on your own?
Have you tried a script?
Have you tried different forms of the ls command?
Have you tried date qualifiers for files, or different regular expressions to filter the output as you'd like it?
You've shown example only and there can be multiple interpretations of what one, singular file you wish to identify out of a directory of many files, and further, your directory may contain a much larger variety of files beyond that limited example, so please think about a better problem description.
And finally, realize that this does appear like a homework or assignment question and if so, it is not policy for LQ responders to provide assistance with those beyond helping you beyond a blocking point, and where you've shown some effort in the first place. So if you really are just experimenting and trying to figure stuff out, go back to the part where you should illustrate some of your efforts, and next offer some explanations on a better scoping of the problem, and also state why you are attempting this particular problem. Some of the questions I've asked above can at least provide you with possible directions in exploring a solution.
|
This is not any assignment. If you can help then let me know the command otherwise its fine. Below is what i did but could not make the desire output. Its shows the format types only but could not display like the one i am looking for
[example@informatica-srv1 ~]$ ls *.* | awk -F\. '{print $(NF);}' | sort | uniq
csv
txt
unl
|
|
|
09-21-2015, 11:42 PM
|
#11
|
LQ Newbie
Registered: Aug 2008
Posts: 20
Original Poster
Rep:
|
Quote:
Originally Posted by HMW
Well, I solved this with a small script (14 lines).
Given a directory that looks like this:
Code:
$ ls -l
totalt 0
-rw-r--r-- 1 HMW HMW 0 sep 21 18:43 alpha20150921_117_212263.unl
-rw-r--r-- 1 HMW HMW 0 sep 21 18:43 alpha20150921_123_033924.unl
-rw-r--r-- 1 HMW HMW 0 sep 21 18:43 charlie13150913_111_222629.unl
-rw-r--r-- 1 HMW HMW 0 sep 21 18:43 charlie13150913_131_349632.unl
-rw-r--r-- 1 HMW HMW 0 sep 21 18:43 tango20150911_117_112263.unl
-rw-r--r-- 1 HMW HMW 0 sep 21 18:43 tango20150911_117_112264.unl
My script produces this output:
Code:
$ ./ls_uniq.sh
-rw-r--r-- 1 HMW HMW 0 sep 21 18:43 alpha20150921_117_212263.unl
-rw-r--r-- 1 HMW HMW 0 sep 21 18:43 charlie13150913_111_222629.unl
-rw-r--r-- 1 HMW HMW 0 sep 21 18:43 tango20150911_117_112263.unl
You might be able to do this with a oneliner, but I am not sure how.
Edit: Please note that I will be more than happy to share my solution as soon as the OP is willing to show some of his/her own efforts.
Best regards,
HMW
|
Below is what i did.
[example@informatica-srv1 ~]$ ls *.* | awk -F\. '{print $(NF);}' | sort | uniq
csv
txt
unl
I am looking from command specially through ls but not through script. Please help
|
|
|
09-21-2015, 11:43 PM
|
#12
|
LQ Newbie
Registered: Aug 2008
Posts: 20
Original Poster
Rep:
|
Quote:
Originally Posted by HMW
^This... is why we are called geeks.
Although this particular geek gave up that contest a long time ago. My code might be longer, but as long as it is easy to read and to modify, I am content.
|
If you can fix it then please let me know the command.
|
|
|
09-21-2015, 11:43 PM
|
#13
|
Senior Member
Registered: Dec 2009
Location: New Jersey, USA
Distribution: Fedora, OpenSUSE, FreeBSD, OpenBSD, macOS (hack). Past: Debian, Arch, RedHat (pre-RHEL).
Posts: 1,335
|
What you did will only print the extension, using the field separator (-F) ".", $NF is the last field, the extension in this case. Did you mean "/" (separate filename from path)?
|
|
|
09-22-2015, 12:53 AM
|
#14
|
Member
Registered: Aug 2013
Location: Sweden
Distribution: Debian, Arch, Red Hat, CentOS
Posts: 773
|
Quote:
Originally Posted by imujjwal
I am looking from command specially through ls but not through script. Please help
|
Well, since my solution IS a script, you will probably be better off asking the awk-ninjas. Although now I am curious myself and will try a oneliner when I get the time!
Best regards,
HMW
|
|
|
09-22-2015, 01:06 AM
|
#15
|
LQ Veteran
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,388
|
Not much chance of a decent answer if the requirements change. First post was "ls -ltr", now "ls *.*" - that changes the input data markedly. And also the result for me as I never (re-)sort a users data unless requested to do so.
|
|
|
All times are GMT -5. The time now is 11:43 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|