How do I concatenate file contents into a single file in bash? [duplicate] - linux

This question already has answers here:
Concatenating multiple text files into a single file in Bash
(12 answers)
Closed 4 years ago.
I have files in different directories under a parent directory, something like this:
Parent Dir
Dir 1 - File 1
Dir 2 - File 2
I want to have an output file that appends the content of File1 with File2. How do I do it in Bash?

Converting my comment to an answer. You can use following find + xargs pipeline command:
cd /parent/dir
find home -name "*.orc" -print0 | xargs -0 cat > test.orc

Related

copy files to multiple directories at once [duplicate]

This question already has answers here:
Linux commands to copy one file to many files
(13 answers)
Closed 2 years ago.
mkdir dir{0..99}
echo hello > file
I want to copy file to every directories, dir0 to dir99.
Currently, the best solution I came up with is:
for i in {0..99}; do cp file dir$i; done
but there must be much more elegant ways to do this.
Is there a way to cp a file to multiple directories using a command similar to below?
cp file dir*
cp file dir{0..99}
You can use xargs to call cp 100 times :
echo dir{0..99} | xargs -n 1 cp file
check the man of xargs

How to write script displaying all subdirectories in the location [duplicate]

This question already has answers here:
List sub-directories with ls [closed]
(3 answers)
Closed 3 years ago.
I need help. How to write script displaying all subdirectories in the location. I've got something like that:
ls -al | grep ^d
but it only works in the home directory
find(1) may be a better choice here:
find . -type d
which would list all directories from the current directory and all subdirectories.

How can I search for specific file contents in all files in both current folder and all subfolders [duplicate]

This question already has answers here:
How do I recursively grep all directories and subdirectories?
(26 answers)
Closed 4 years ago.
Using Mac terminal server linux/bash commands, how can I search for a particular text string in all *.txt files in the current folder plus all files in the subfolders inside the current folder?
grep -i "xxx" */*
xxx is the target text I am trying to find.
find . -type f -print | egrep ".txt$" | xargs grep "SearchPatern"
Explained as
Find all the file names in the current directory and below send to ....
grep which picks out the file name that end in .txt and send names to ....
xargs which will execute a grep command on each file to look for SearchPatern.

shell command to extract the part of filename having characters? [duplicate]

This question already has answers here:
Extract filename and extension in Bash
(38 answers)
In Bash, how to strip out all numbers in the file names in a directory while leaving the file extension intact
(1 answer)
Closed 5 years ago.
I have a file named(multi_extension123.txt). Before copying this file into the directory I need to remove files like
[multi_extension1234.txt
multi_extension1234.txt
multi_extension12345.txt] if present in the directory and then copy the earlier one to this directory. Can anyone give the solution with shellscript.
note: i need to remove only numerical numbers and extension alone.
I have tried this
$ filename= $file1
$ echo "${filename%[0-9].*}"
find . -type f maxdepth 0 mindepth 0 -name "'$filename'[0-9]*.txt" -exec rm -f {} \;

how to list full paths of folders inside a directory in linux? [duplicate]

This question already has answers here:
Show full path when using options
(8 answers)
Closed 8 years ago.
I have a folder /home/Documents/myFolder and inside this folder there are lots other folders. I want to have a file list.txt which contains all the paths of the folders. I want the text file content like this:
/home/Documents/myFolder/1234
/home/Documents/myFolder/asd2
/home/Documents/myFolder/asdawgf
/home/Documents/myFolder/dawt
.
.
.
I tried this one but it was not what I want ls > /home/Documents/myFolde/list.txt
it just prints the folder names. I want the full paths.
Use find listing all directories (-type d) and then sed the output to get the full path correct:
find . -type d | sed -n 's:^\./:/home/Documents/myFolder/:'p > /home/Documents/myFolder/list.txt
you can use find:
find . > /home/Documents/myFolde/list.txt

Resources