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.
First, you get the error because you are using a text editor for this. This means that every time you open the file, edit and save it, the original is overwritten with the new contents. Whether you added a single line to the end or 100 lines all over the place is irrelevant, the point is that the file is being opened, edited and saved and that overwrites the contents.
For example, compare your workflow to running this:
for((i=0;i<20;i++)); do echo $i >> file.txt; sleep 1; done
That will write a number to file.txt
every second for twenty seconds. If you open another terminal and run tail -fn 0 file.txt
, you will see the output you expect.
So, to get the desired behavior add the text to the file using >>
from a terminal instead of manually editing it.
More details
nano
seems to be the odd one out here. Most editors when opening a file then saving it will actually delete the original file and save a new one with the same name. You can test this by checking the inode number of the file:
$ ls -il file.txt
16647801 -rw-r--r-- 1 terdon terdon 9 Apr 6 18:19 file.txt
Files are simply hardlinks to specific inodes, in this case, file.txt
points to inode 16647801
. Now, open the file in gedit
, add a line and check the inode again:
$ gedit file.txt
$ ls -il file.txt
16647854 -rw-r--r-- 1 terdon terdon 13 Apr 6 18:23 file.txt
As you see, the inode number has changed, in other words, the original file was removed and a new one was created. nano
does not do that, trying the same thing with nano
does not change the inode. It does, however, delete the original contents overwriting them with the new contents. That's why tail
actually shows the output, if you try it and edit the file with gedit
(or emacs
or a number of other editors), the extra lines you add won't be shown in the output of tail
at all.
Best Answer
What about: