list only folders (ls -d) relative to parent in bash - bash

I have found that this command works correctly in the current folder:
ls -d */ -1
but how can I get the same result in case when I want to list only folders being not in this folder (e.g. I want to list folders being on 1 or 2 levels higher, that is, in a parent or grandparent).

Sounds like you're looking for find, along with the -type d switch to limit the results to directories:
find .. -type d
You can use .. to start from the parent directory.
Depending on your version of find, you may be able to specify -mindepth and -maxdepth to limit the results:
find .. -mindepth 1 -maxdepth 2 -type d

Related

How to understand how this find with while loop works in Bash

How can I understand this syntax a bit better?
find ~/Documents/project/updates -type d -name "Branch*[0-9]" -maxdepth 1 -mtime -2 -print |\
while read path; do
dir_name=$(basename $path)
function_call $dir_name
done
Generate list of directories with find
~/Documents/project/updates look for directories under this path
-type d look only for "directories" (and not files, inodes, or other file types)
-name "Branch*[0-9]" look only for directories whose names match this wildcard
maxdepth 1 don't look any lower than one level deep
-mtime -2 modification time -2 days
Now that we have a list, for each item in that list,
dir_name=$(basename $path) Set "dir_name" to the basename of the directory
function_call $dir_name Call "function_call" with "$dir_name"
STRONG SUGGESTION:
Temporarily add set -xv to the top of your shell script and observe the results :)

Using cp -ur, but update only directories

I have two directories that I want to contain mostly the same information.
I wrote the basic script
#!/bin/bash
# Update the two folders
cp -vur ../catkin_ws/src .
cp -vur . ../catkin_ws/src
Now I want to change this, to only update the directories and their content but not other files on the top level directory, like the bash script itself.
If that is not possible, is there a way to exclude certain files during the update?
Suppose you want to synchronize ../catkin_ws/src with the current directory, and the current script is located in the current directory. As far as I understand, you want to synchronize only the top-level directories including their contents, but not other types of nodes possibly located at the top level, i.e. directly within ../catkin_ws/src, or ./.
Then it is easily done with find command:
src_dir="../catkin_ws/src"
find "$src_dir" -mindepth 1 -maxdepth 1 -type d \
-exec cp -vru {} . \;
find ./ -mindepth 1 -maxdepth 1 -type d \
-exec cp -vru {} "$src_dir" \;
where {} stands for the next directory found.
If you want to filter further, you may use extra options such as -name, -path, or -regex. For example, the following skips directory x:
find "$src_dir" -mindepth 1 -maxdepth 1 -type d \
! -name 'x' \
-exec cp -vru {} . \;
where ! is causes the next expression to return True, if the expression is false, i.e. acts as some kind of logical NOT operator.
The drawback of the above commands is that the cp command is launched for each folder sequentially (due to \;). If you want to run single cp command for all sources, you may use an approach described in this answer.
P.S.
I didn't try to suggest better way to synchronize the directories, but only suggested a way to fix your current approach.

issue facing with find command in linux

I am facing one problem using find command. I have below directory structure. In which a,b,c directories contain some files but i want to scan only b directory when i am using below command it scans c directory as well which is not required.
/a/b/c/
find /a/b/ f -type -mtime +90
Is there any solution to resolve this problem?
Please assist.
i want to write a find command which scans a directory for files but does not scan files of its subdirectories.
Use -maxdepth 1 to look in the b directory only.
find /a/b/ -type f mtime +90 -maxdepth 1
From man find:
-maxdepth levels
Descend at most levels (a non-negative integer) levels of directories below
the command line arguments. '-maxdepth 0' means only apply the tests and
actions to the command line arguments.
find /a/b/ -maxdepth 1 -type f -mtime +90

Bash script that deletes files older than N days but excludes files in certain folder

I need to create a bash script that deletes all files older than N days in downloads folder but would exclude all files in archive sub-folder. My folder structure is like this:
downloads/
user1_folder/
archive/
user2_folder/
archive/
...
Based on this Q&A I was able to create script that finds and deletes files older than N days, but I would like to exclude all files in archive subfolders.
#!/bin/bash
find ./downloads -mtime +32 -type f -delete
Try:
find ./downloads -maxdepth 2 -type f -mtime +32 -delete
-maxdepth levels
Descend at most levels (a non-negative integer) levels of directories below the command line arguments. -maxdepth 0
means only apply the tests and actions to the command line arguments.
Adding ! -path (your path) should do the trick
find ./downloads ! -path ./downloads/*/archive/* -mtime +32 -type f -delete

In shells, how to find a file in current directory with at least 2 characters but not contain cs by using echo

In shells, how to find a file in current directory whose name has at least 2 characters but not contain cs by using echo.
Try this.
$ find . -name "*??*" -not -name "*cs*"
If you are interested only in the files in the current directry, and don't want to traverse to subdirectories, try the following.
$ find . -maxdepth 1 -name "*??*" -not -name "*cs*"

Resources