Find-based solution:
find . -maxdepth 1 -type f -printf '%f\n'
Bash-based solution:
for f in *; do [[ -d "$f" ]] || echo "$f"; done
## or, if you want coloured output:
for f in *; do [[ -d "$f" ]] || ls -- "$f"; done
The bash-based solution will get you everything that isn't a directory; it will include things like named pipes (you probably want this). If you specifically want just files, either use the find
command or one of these:
for f in *; do [[ -f "$f" ]] && echo "$f"; done
## or, if you want coloured output:
for f in *; do [[ -f "$f" ]] && ls -- "$f"; done
If you're going to be using this regularly, you can of course put this into an alias somewhere in your ~/.bashrc
:
alias lsfiles='for f in *; do [[ -f "$f" ]] && ls -- "$f"; done'
Since you noted in the comments that you're actually on OSX rather than Ubuntu, I would suggest that next time you direct questions to the Apple or more general Unix & Linux Stack Exchange sites.
I had a script that I adjusted (good idea the (N+1)q part!) thanks to @chronitis comment and the SO answer:
#! /bin/bash
#
N=10
M=20
P=2
Q=3
sed -n "$N,${M}p; $((M+1))q" $1 | cut -d, -f$P-$Q
Save the file as for example cut_csv
, make it executable and use as
cut_csv file
It can be made fancier by accepting the N,M,P,Q parameters as input etc, but I use it seldomly so I normally simply edit the file.
How it works:
The main command is the following (let's suppose N=10, M=20, P=2, Q=3); the shell substitutes the variables and the last line will become: (1)
sed -n "10,20p ; 21q" file | cut -d, -f2-3
Let's start with the first command:
sed -n "10,20p ; 21q" file
This call sed (stream editor, man sed
) in no-print mode (-n
) and execute the following commands on the file:
- print (
p
) the lines between 10 and 20 (this is the 10,20p
part)
- quit (
q
) when reading line 21 (21q
) so that discard the rest of the file
The output of sed is piped (|
) to cut
:
cut -d, -f2-3
This command (man cut
) selects fields of a line (and repeat for each line). In this case, I am telling it that the separator between fields (columns) is a commad (-d,
), and to print out the columns between 2 and 3.
As another more complex example I often use this one:
sed -n "1p; 10,14p; 21q" data.csv | cut -d, -f1,4-8
This will select row 1 (where I have titles :-)) and rows from 10 to 14 (5 lines); then select columns 1 (time in my data...) and column from 4 to 8. It is really powerful once you get grips with it.
(1) one great way to see what the shell is doing is change the first line (which is called a shebang) like that:
#! /bin/bash -xv
The shell will now print every command it reads and the result of the substitutions:
(0)asus-rmano: part_of_csv.sh p20dedo.csv
#! /bin/bash -xv
#
N=10
+ N=10
M=20
+ M=20
P=2
+ P=2
Q=3
+ Q=3
sed -n "$N,${M}p; $((M+1))q" $1 | cut -d, -f$P-$Q
+ cut -d, -f2-3
+ sed -n '10,20p; 21q' p20dedo.csv
16:05:49,000
16:05:51,000
[...]
Best Answer
This creates a CSV file
listing.csv
with file name, time stamp and size for all files in/some/folder/
and its subfolders:See the documentation for
-printf
in the manpage for find if you want to use other fields.Note that it doesn't work for file names containing
"
characters.