ls

Listing files in date order with spaces in filenames

房东的猫 提交于 2019-11-28 11:50:22
I am starting with a file containing a list of hundreds of files (full paths) in a random order. I would like to list the details of the ten latest files in that list. This is my naive attempt: $ ls -las -t `cat list-of-files.txt` | head -10 That works, so long as none of the files have spaces in, but fails if they do as those files are split up at the spaces and treated as separate files. File "hello world" gives me: ls: hello: No such file or directory ls: world: No such file or directory I have tried quoting the files in the original list-of-files file, but the here-document still splits

Delete all files except the newest 3 in bash script

佐手、 提交于 2019-11-28 09:53:27
Question: How do you delete all files in a directory except the newest 3? Finding the newest 3 files is simple: ls -t | head -3 But I need to find all files except the newest 3 files. How do I do that, and how do I delete these files in the same line without making an unnecessary for loop for that? I'm using Debian Wheezy and bash scripts for this. This will list all files except the newest three: ls -t | tail -n +4 This will delete those files: ls -t | tail -n +4 | xargs rm -- This will also list dotfiles: ls -At | tail -n +4 and delete with dotfiles: ls -At | tail -n +4 | xargs rm -- But

Preserve ls colouring after grep'ing

空扰寡人 提交于 2019-11-28 05:23:57
If I do $ ls -l --color=always I get a list of files inside the directory with some nice colouring for different file types etc.. Now, I want to be able to pipe the coloured output of ls through grep to filter out some files I don't need. The key is that I still want to preserve the colouring after the grep filter. $ ls -l --color=always | grep -E some_regex ^ I lose the colouring after grep EDIT: I'm using headless-server Ubuntu 8.10, Bash 3.2.39, pretty much a stock install with no fancy configs lhunath Your grep is probably removing ls 's color codes because it has its own coloring turned

Unix ls command: show full path when using options

半腔热情 提交于 2019-11-28 03:15:09
I often use this list command in Unix (AIX / KSH): ls -Artl It displays the files as this: -rw-r--r-- 1 myuser mygroup 0 Apr 2 11:59 test1.txt -rw-r--r-- 1 myuser mygroup 0 Apr 2 11:59 test2.txt I would like to modify the command such a way that the full path of the file is displayed. For example: -rw-r--r-- 1 myuser mygroup 0 Apr 2 11:59 /usr/test1.txt -rw-r--r-- 1 myuser mygroup 0 Apr 2 11:59 /usr/test2.txt Any ideas? I found several resolution methods using pwd or find but - as far as I see - this does not work work if I want to keep the ls options. What about this trick... ls -lrt -d -1

How to create ls in windows command prompt?

限于喜欢 提交于 2019-11-28 02:53:24
I want to use ls in windows command prompt and make it run the dir command. How can I do that? You could: create a batch file called ls.bat and have it contain the dir command only add the directory where the ls.bat file exists to your PATH environment variable You could then execute ls from a command prompt. secghost You can solve this question with one simple command: echo @dir %* > %systemroot%\system32\ls.bat Make sure you run cmd.exe as admin first if you are on vista and up Its an old question but for the record: http://gnuwin32.sourceforge.net/packages/coreutils.htm Gives you ls and a

How can I list (ls) the 5 last modified files in a directory?

假装没事ソ 提交于 2019-11-28 02:51:19
I know ls -t will list all files by modified time. But how can I limit these results to only the last n files? Try using head or tail. If you want the 5 most-recently modified files: ls -1t | head -5 The -1 (that's a one) says one file per line and the head says take the first 5 entries. If you want the last 5 try ls -1t | tail -5 Use tail command: ls -t | tail -n 5 The accepted answer lists only the filenames, but to get the top 5 files one can also use: ls -lht | head -6 where: -l outputs in a list format -h makes output human readable (i.e. file sizes appear in kb, mb, etc.) -t sorts output

Sort files numerically in bash

时间秒杀一切 提交于 2019-11-27 23:08:08
I need to sort .flv files numerically and i was able to do it with the following command: ls *\.flv | sort --version-sort -f but with many files(hundreds) it's not sorting correctly. ls *\.flv | sort --version-sort -f | tail -n 20 e680.flv e681.flv e682.flv e683.flv e684.flv e685.flv e686.flv e687.flv e688.flv e689.flv e690.flv e691.flv e692.flv e693.flv e694.flv e695.flv **e696.flv** s572.flv s602.flv s654.flv but the strange this is, if i'm ruining the command without "*.flv" it's working. i could use just ls but i have other file types in the folder. ls | sort --version-sort -f | tail -n 20

What does the dot at the end of the permissions in the output of “ls -lah” mean?

丶灬走出姿态 提交于 2019-11-27 22:37:57
问题 I found some Linux files, and when I type ls -lah , it outputs this permissions format: ... drwxr-xr-x. 2 root root ... -rw-rw-r--. 1 root root ... I would like to know, what is the meaning of the dot ( -rw-rw-r--. ) at the end of the permissions format? 回答1: From info coreutils 'ls invocation' under Linux GNU `ls' uses a `.' character to indicate a file with an SELinux security context, but no other alternate access method. A file with any other combination of alternate access methods is

How to print time_t in a specific format?

China☆狼群 提交于 2019-11-27 18:19:25
问题 ls command prints time in this format: Aug 23 06:07 How can I convert time received from stat() 's mtime() into this format for local time? 回答1: Use strftime (you need to convert time_t to struct tm* first): char buff[20]; struct tm * timeinfo; timeinfo = localtime (&mtime); strftime(buff, sizeof(buff), "%b %d %H:%M", timeinfo); Formats: %b - The abbreviated month name according to the current locale. %d - The day of the month as a decimal number (range 01 to 31). %H - The hour as a decimal

How do I assign ls to an array in Linux Bash?

北战南征 提交于 2019-11-27 17:56:43
array=${ls -d */} echo ${array[@]} I have three directories: ww ee qq . I want them in an array and then print the array. It would be this array=($(ls -d */)) EDIT: See Gordon Davisson's solution for a more general answer (i.e. if your filenames contain special characters). This answer is merely a syntax correction. Whenever possible, you should avoid parsing the output of ls (see Greg's wiki on the subject ). Basically, the output of ls will be ambiguous if there are funny characters in any of the filenames. It's also usually a waste of time. In this case, when you execute ls -d */ , what