Home Finding a filetype in current directory and subdirectory, including hidden files. (Homework)
Reply: 1

Finding a filetype in current directory and subdirectory, including hidden files. (Homework)

Skylar Gordon
1#
Skylar Gordon Published in 2018-01-12 21:58:57Z

With bash, I am needing to find all gifs in my current directory and subdirectories, and display them in a specific way. I have to include hidden files as well, and I am not allowed to use grep (or its subsidiaries - fgrep and such) or basename. I am running Ubuntu 14.04 through a virtual machine (VirtualBox via Vagrant if that matters) without a GUI. My current script looks like this:

#!/bin/bash
ls -a | find $directory -type f -name "*.gif" | rev | cut -d/ -f1 | rev | cut -d. -f1 | sort -f

This has mostly done what I need, especially with regard to formatting, but when I changed one of the gifs to a hidden file, it was no longer visible - there was an extra empty line, almost as if the file name was written in invisible ink. Does anyone know why it's doing this?

rkta
2#
rkta Reply to 2018-01-15 07:21:19Z

It's not very clear what you are trying to achieve.

As lurker said in the comments cut -d. -f1 will make any line starting with a . be a blank line.

From your code, the closest I could think of is

find $directory -type f -iname '*.gif' | rev | cut -d/ -f1 | cut -d. -f2,3 | rev | sort -f

Giving you all gifs, hidden or not, without path or extension.

Example

user@host /tmp % ls -aR
.:
.  ..  subdirectory  .test.gif  test.gif

./subdirectory:
.  ..  .sub.gif  sub.gif
user@host /tmp % find . -type f -iname '*.gif' | rev | cut -d/ -f1 | cut -d. -f2,3 | rev | sort -f
sub
.sub 
test
.test
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.318203 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO