Count files and folders in directory (ignore hidden files) [duplicate] - bash

This question already has answers here:
Recursively counting files in a Linux directory
(24 answers)
Closed 4 years ago.
This has probably answered before, but I cannot find it.
How do I count the files and directories in a directory without including subdirectories? Also, hidden files should be ignored (because this folder is a git repo).
More precisely, I need an if clause, that the current folder only has one file (namely "script.sh") and no sub-folders with the exception of .git/ .
How can I do this in Bash?
EDIT: In contrast to Recursively count specific files BASH, I want to ignore the .git folder and I do not want to count files and folders in subfolders.

find should help:
if [ "$(cd /some/dir && find * -maxdepth 0 -type f)" == "script.sh" ]; then
* will list only non hidden files and maxdepth won't search subdirectories.

Related

making text files containing list of files in different directories with random name in bash

I have a script which makes some files in pair (all the resulting files have either R1 OR R2 in the file name) and then put each pair in a separate directory with random name (every time will be different and I cannot predict). for example if I have 6 files, 3 files would be file_R1.txt or file_R2.txt but in pair like this example:
s1_R1.txt and s1_R2.txt
s2_R1.txt and s2_R2.txt
s3_R1.txt and s3_R2.txt
In this example I will have 3 directories (one per pair of files). I want to make 2 text file (d1.txt and. d2.txt) containing above file names. In fact, d1.txt will have all the files with R1 and d2.txt will contain all the files with R2. To do so, I made the following short bash code but it does not return what I want. Do you know how to fix it?
For file in /./*R*.txt;
do;
touch "s1.txt"
touch "s2.txt"
echo "${fastq}" >> "s1.txt"
done
Weird question, not sure I get it, but for your d1 and d2 files:
#!/bin/bash
find . -type f -name "*R1*" -print >d1.txt
find . -type f -name "*R2*" -print >d2.txt
find is used since you have files under different sub-directories.
Using > ensures the text files are emptied out if they already exist.
Note the code you put in your question is not valid bash.

Copy files from nested directories to similar nested directories

I have files under nested directories like
/x/0/0/0/a.txt till /x/9/9/9/a.txt
To be more specific
Under /x/ there are 0-9 folders
Under /x/0/ there are 0-9 folders
Under /x/0/0/ there are 0-9 folders
Under/x/0/0/0/ there are files like a b c.
Now all these files needed to be copied to a directory like
/y/ , where the previous directory structure should be followed.
Basically simplest "cp -r" is doing this task;
If you need to copy directory including "x"
cp -r /x /y/
Otherwise if you need only folders inside of "x"
cp -rp /x/* /y/

Find Count of .XML Files in Directory / Sub-Directory

I have a directory with millions of sub directories with millions of sub directories and each directory may contain .XML files or another sub directory ... what I need is to count the number of .XML files in the parent directory in UNIX ...
I've tried a lot of this but ended up with listing them not counting them - Any ideas ??
I'm guessing you're looking for the number of xml files on the children directories, not parent. If so, this should do the trick:
find . -type f -name \*.xml | wc -l
Make sure you're on the top level directory from where you want to search. It will display all files named *.xml and count the returned lines.

Shell Script for searching Makefile recursively in directories

I need to build a shell script on debian based OS to recursively browse and identify which Folders have Makefile present. If present then build the package. If not present then just list those folders. The catch here as shown below is I need to browse only one folder below the parent folder (ABC) and check if makefile is present under Folder1, Folder 2...etc and not to go into the sub directories of Folder1 (not to look for Makefile under folders Folder1.1, Folder 1.2, Folder2.1 etc). Looking for some tips how to loop only one level and then exit back to folder ABC and start the search.
ABC---
|---Folder1
| |-------Makefile
|-------Folder1.1
|-------Folder1.2
|---Folder2
| |-------Somefile
|-------Folder2.1
|-------Folder2.2
|---FolderN
| |-------Makefile
|-------FolderN.1
|-------FolderN.2
As answered by Karthikraj in above comments. This helped
find . -maxdepth 2 -type f -iname 'makefile'

How do I record the name of the files in a folder? [duplicate]

This question already has answers here:
How do I read in the contents of a directory in Perl?
(9 answers)
Closed 9 years ago.
for example i have :
file directory: C:\Users\Desktop\program
files in directory: cheese(folder) ,veggie(txt file), meat(doc file)
How do i record down the names of the files and folder in the directory?
Thanks in advance!!
Another way is to use glob:
my #files = <*>;
This will give you all of the files in the current directory.
This method is convenient if you only want to find certain files:
my #jpegs = <*.jpg>;
If that isn't the directory you want, change to the current directory or include the path:
my #files = </foo/bar*>;
Note that you should use forward slashes even if doing this on Windows.
Try the following:
opendir FOLDER, $folder or die "Cant open folder $folder: $!";
my #file= readdir FOLDER;
closedir FOLDER;

Resources