See if directory rec is used as symlink in Linux - linux

I want to see, if a symlink points to a directories in a specific dir - recursively.
Of course, I clould use
find / -type l -ls 2>/dev/null |grep /targetpath
But I do not want type all the (recurse) paths.
So I put all symlinks on my system into a file once.
find / -type l -ls 2>/dev/null >~/symlinks.txt
Then I list the directories recursively.
find /targetpath to start/ -maxdepth 2 -type d
And that is my question:
Can I pipe these paths from the last command to grep?
Grep should look into my file symlinks.txt and show the linecontent of matching lines (could be more symlinks pointing to this DIR)
I tried something like
find /targetpath to stat/ -maxdepth 2 -type d | xargs -0 -ifoo grep foo symlinks.txt
But it does not do, what I expect.
Or maybe an other, better solution?

From man find:
-lname pattern
File is a symbolic link whose contents match shell pattern pattern. [...]
Try:
find / -lname '*/targetpath/*'
See find-all-symlinks-to-a-directory-and-change-target-to-another-directory.

Related

Linux - Print only filenames for the directory and sub-directories

I'm very new at Linux world.
I've the following directories on my linux (centos rhel fedora"):
Folder_Root:
/root/main
/root/main/files
/root/main/files/file_1.txt
/root/main/files/file_2.ssh
/root/main/files/file_2.txt
/root/main/file_3.txt
I'm trying to make a print of all the files in all the directories. Basically I am trying to get the following list:
file_1.txt
file_2.ssh
file_2.txt
file_3.txt
I already try 'ls' command and 'ls -al': but it prints also the direcotry name.
I also try to use 'ls -lR | more': but it prints a lot of details that I don't want to use.
Do you recommend any command?
How about using:
find . -type f -exec basename {} \;
or even:
find . -type f -printf "%f\n"
There is a similar question asked here and it has many answers, hope this helps:
List only file names in directories and subdirectories in bash
How about using find:
find /root/main -type f

Counting number of files in a directory with an OSX terminal command

I'm looking for a specific directory file count that returns a number. I would type it into the terminal and it can give me the specified directory's file count.
I've already tried echo find "'directory' | wc -l" but that didn't work, any ideas?
You seem to have the right idea. I'd use -type f to find only files:
$ find some_directory -type f | wc -l
If you only want files directly under this directory and not to search recursively through subdirectories, you could add the -maxdepth flag:
$ find some_directory -maxdepth 1 -type f | wc -l
Open the terminal and switch to the location of the directory.
Type in:
find . -type f | wc -l
This searches inside the current directory (that's what the . stands for) for all files, and counts them.
The fastest way to obtain the number of files within a directory is by obtaining the value of that directory's kMDItemFSNodeCount metadata attribute.
mdls -name kMDItemFSNodeCount directory_name -raw|xargs
The above command has a major advantage over find . -type f | wc -l in that it returns the count almost instantly, even for directories which contain millions of files.
Please note that the command obtains the number of files, not just regular files.
I don't understand why folks are using 'find' because for me it's a lot easier to just pipe in 'ls' like so:
ls *.png | wc -l
to find the number of png images in the current directory.
I'm using tree, this is the way :
tree ph

Bash list directories that matches pattern, not children of them

I have a folder ~/anna which contains the file ~/anna/b
When I type ls ~/a* I get b.
How can I retrieve ~/anna ?
The script for recreating the scenatrio:
cd ~/
mkdir anna
touch anna/b
ls ~/a*
Expected result: anna
Actually result: b
Thanks!
To get help for the ls, just ask for it:
ls --help
You'll get list of useful options for the ls command, one of them:
-d, --directory list directory entries instead of contents,
and do not dereference symbolic links
So the solution (as stated in comments) would be:
ls -d ~/a*
Depending on your different requirements, find might be more appropriate:
find ~/ -name "a*" -type d
or
find ~/ -mindepth 1 -maxdepth 1 -name "a*" -type d
explanation:
~/: search in home dir
-mindepth 1 -maxdepth 1: only directories "one deep"
-name "a*": all files or folders starting with a
-type d: find only directories

pipe specific list of gziped log files into zgrep

I'm having trouble with getting a list of the lines in a bunch of gzipped apache access log files. What I want is to get a list of the log files numbered 1 and 2 only, then grep through them and extract the lines with specific matching text.
I originally got this to work just for access log archives numbered 1. The "/pathname" text was the text I was looking for:
zgrep /pathname/ access_*.log.1.gz
Since ls does not support regex, I came up with the following to get a listing from the current directory of the files I want:
find . -maxdepth 1 -type f -regex '\./access.+\.log\.[1|2]\.gz' -printf '%P\n'
find . -maxdepth 1 -type f -regex '\./access.+\.log\.[1|2]\.gz' | sed "s|^\./||"
My problem now is taking that file list output and zgrepping through the files to return lines within those files that match my text. Am I barking up the wrong tree here?
Try:
zgrep /pathname/ access_*.log.{1,2}.gz
Alternatively, use find -exec:
find . -maxdepth 1 -type f -regex '\./access.+\.log\.[1|2]\.gz' -exec zgrep /path/ {} \;
I don't have apache-logs, so I use a similar, but not identical pattern:
ls /var/log/*.[12].gz
The shell doesn't support regex, but grouping with [123] or [1-3], as well as {1,2,3} and {1..3} or even {o..w} and {066..091}.

List all files in a directory with abosolute paths (excluding directories)

I've currently got:
ls -1 $(pwd)/*
Gives me all the files in a directory with absolute paths - but formats it with the directory at the start of each list of files.
Is there a way just to get a list of files in a directory recursively (absolute paths) - excluding the directory/sub-directories themselves?
find $(pwd) -type f -print
or
find $(pwd) -type f -ls
If you are feeding it into something else, you might want -print0 (to handle filenames with spaces).
E.g.: find . -type f -print0 | xargs --null --no-run-if-empty grep

Resources