How to find all available links for a particular file in Linux - linux

I have a file named testfile.txt in /home/rajeesh/Desktop/My/Assignments/. I need to search for all the available links to the above-mentioned file using some CLI command.
I tried:
find -L /home -inum $(ls -di testfile.txt | cut -d" " -f1)
I am running this command in directory /home/rajeesh/Desktop/My/Assignments/ but the result contains only link available in the current directory (I have links for the file on Desktop and in My folders).
Could anybody help me with this, please?
Thanks in advance.

If using GNU find:
find -L /home -samefile /home/rajeesh/Desktop/My/Assignments/testfile.txt
will display all files existing in the /home directory tree that have the same inode as the given file (Meaning they're hard links to the same underlying file) or are symbolic links pointing to the given file.

Related

How to search for all the hidden files in my computer?

I want to find all the hidden files inside a directory in linux terminal.
I have found out that we have a grep command to search for the file but I need to search for hidden files.
grep -r search *
Any help would be appreciated. Thanks.
try this on your terminal too show all the hidden files on your system:
find / -name ".*" 2> /dev/null
or you can use other way like in this web https://devconnected.com/how-to-show-hidden-files-on-linux/
Simply use (with GNU grep)
grep -r search .
if you want to search contents of files in the current directory and its subdirectories recursively.
Note: It isn't clear if you want to search filenames or contents of files.
The proper solution:
find /dir -name '.*' -type f
If by "hidden file" you mean Linux file names that begin with . that are often hidden by default, (and directories starting with . whose contents might also be considered "hidden") then try this command:
find . -print | grep '/\.'

Search for file or folder containing a keyword in Linux

I want to find a file or folder containing a keyword. How can I do this from the command line? I am starting in a directory that I know contains this keyword somewhere, but am not sure where. It could be several directories down or could be just a couple.
I don't want to find the keyword inside a file, I want to find a directory or filename that includes the keyword.
cd to directory and give command:
find | grep -i 'keyword'
Use grep,
$> grep -r "keyword" /path/to/find/the/fil
If you want to find it from current location,
$> grep -r "keyword" .
man grep for detail usage.

How can I export a recursive directory & file listing to a text file in Linux Bash shell with an SSH command?

Assume I'm in my pwd - /home/kparisi/
What command can I run to export all directories & files from this directory and all subdirectories within it to a text file?
I don't need the contents of the files, just their names & paths (and permissions if possible)
Thanks in advance.
Use find to get a listing of all the files in a directory and its subdirectories, then pipe it to a file with the > operand.
find > list.txt
find is a good utility to obtain (recursively) all the content of a directory. If you want the file (and directory) names with their permissions:
find /home/kparisi -printf "%M %p\n"
You can then use ssh to run this command on the remote server:
ssh kparisi#remote.com 'find /home/kparisi -printf "%M %p\n"'
And finally, if you want to store this in a file on your local server:
ssh kparisi#remote.com 'find /home/kparisi -printf "%M %p\n"' > file
I understand that it is an old question, but anyway if somebody will reach this page, maybe will be useful. I love to do this (assume we have the file "fsstruct.txt" to save directory structure):
tree > fsstruct.txt
for me this format is simpler to read.
It is easy also to use other features of tree:
tree -p > fsstruct.txt
prints file type and permissions before file and it is more accessible when you are reading plain text, which is a file and which is a directory.
tree -h > fsstruct.txt
this:tree -ph > fsstruct.txt
prints sizes of files in a human readable format.
Also, it is possible to send output to the file: tree -ph . -o fsstruct.txt or create HTML:
tree -Hph . -o tree.html
The command find > list.txt will do. If you want directory tree list tree -a > list.txt can be used

Retrieving the sub-directory, which had most recently been modified, in a Linux shell script?

How can I retrieve the sub-directory, which had most recently been modified, in a directory?
I am using a shell script on a Linux distribution (Ubuntu).
Sounds like you want the ls options
-t sort by modification time, newest first
And only show directories, use something like this answer suggests Listing only directories using ls in bash: An examination
ls -d */
And if you want each directory listed on one line (if your file/dirnames have no newlines or crazy characters) I'd add -1 So all together, this should list directories in the current directory, with the newest modified times at the top
ls -1td */
And only the single newest directory:
ls -1td */ | head -n 1
Or if you want to compare to a specific time you can use find and it's options like -cmin -cnewer -ctime -mmin -mtime and find can handle crazy names like newlines, spaces, etc with null terminated names options like -print0
How much the subdirectory is modified is irrelevant. Do you know the name of the subdirectory? Get its content like this:
files=$(ls subdir-name)
for file in ${files}; do
echo "I see there is a file named ${file}"
done

LINUX: How do I search for a string in a different directory?

I am working on the Linux terminal and I want to know how to search for c files in a certain directory. I have it working for the current directory but how do I do it for the directory usr/
for the current directory i did
grep -l "main" *.c
so I'd like to know how to search in the /usr directory without changing the directory first. Thanks.
You should just be able to prefix the directory name you want to the file pattern you want to search.
grep -l "main" /usr/*.c
You could also use:
find /usr/ -name "*.c"
if it is just the C files in the /usr/ directory you are after.

Resources