Is it safe to alias cp -R to cp? - bash

When I copy something, I always forget the -R, then I have go all the way back to add it right after cp.
I want to add this to bash config files.
alias cp="cp -R"
I have not seen anything bad happen. Is it safe to do this?

The only thing I can think of that would cause unexpected behavior with the -R flag is that it doesn't work with wildcards.
What I mean is... for example you want to copy all mp3 Files in a directory and every subdirectory with: cp -R /path/*.mp3. Although -R is given it will not copy mp3s in the subdirectories of path - if there are any.

I wouldn't use aliases for changing the behaviour of normal commands. When you are in a different shell / another computer, the alias will be missing. When a friend wants to help you, he will not know what you did.
Once I had an alias rm="rm -i" and I performed rm *, while I just had changed shell with a su.
And sometimes you want to use cp without the -R option, will you remember to use /bin/cp in these cases (copy all files in the current dir to another location, but do not cp the subdirs)?

Related

Using functions as an argument in Bash

I want to move a couple of files from point a to point b
but I have to manually specify
mv /full/path/from/a /full/path/to/b
but some times there are 20 files which I have to move manually. Instead of /full/path/form/a, can't I just enter the a function which returns all the files which I want to move in my case;
/full/path/to/b is a directory, it's the target directory which all the files with extenstions mp3, exe and mp4 must go to:
mv ls *.{mp3,exe,mp4} /full/path/to/b
If I have to move a couple of files and I don't want to do it one by one, how can I optimize the problem?
The command mv ls *.{mp3,exe,mp4} /full/path/to/b in your question is not correct.
As pointed out in comments by #janos, the correct command is
mv *.{mp3,exe,mp4} /full/path/to/b
mv can complain about missing file if the file is really missing and/or the path is not accessible or is not valid.
As i can understand by your question description, if you go manually to the source path you can move the file to the desired directory.
Thus it seems that path is valid, and file exists.
In order mv to keeps complaining about *.mp3 not found (having a valid path and file) the only reason that pops up in my head is the Bash Pathname Expansion feature (enabled by default in my Debian).
Maybe for some reason this pathname expansion bash feature is disabled in your machine.
Try to enable this feature using command bellow and provide the correct command to mv and you should be fine.
$ set +f
PS: Check man bash about pathname expansion.

How to overwrite the empty files in a directory?

I am new to shell scripting.How to overwrite the empty files in a directory using shell scripting?
Can anyone help me above question...
Your question is pretty unspecific, please add tags and explain what scripting language you mean (eventually tell the OS you're using it).
With bash, you can overwrite files like this:
With mv: mv -f sourcefile /path/to/existing/destfile
With cp: cp overwrites files per default. To disable that behaviour, use the -n option
If the files are really empty, I guess you overwrite them not for resetting the content, but change the times (atime, mtime ...)
If it is true, touch is the tool to use. man touch to learn the usage.
If you have a source file, you can just cp or mv as #weeheavy suggested.

How to move files of "specific extension" ( from directories ) to a new location while maintaining full directory structure ?

I have following directory structure :
/home/dir1/abc.jpg
/home/dir1/abc.pdf
/home/dir1/dir2/abc.jpg
/home/dir1/dir2/abc1.jpg
/home/dir1/dir2/dir3/abc.jpg
and I want to copy jpg files from them to a new folder which will have same directory structure, for eg.:
/home/newdir1/abc.jpg
/home/newdir1/dir2/abc.jpg
/home/newdir1/dir2/abc1.jpg
/home/newdir1/dir2/dir3/abc.jpg
How to achieve it using rsync or any other software ?
Please help, Many Thanks !!
From the looks of what you've included in your question, there are a couple of things you might try.
You've specified that you want to "move" files. That means you either use the mv command, or use rsync's --remove-source-files option. For example:
mv /source1/* /source2/* /path/to/targetdir/
or
rsync -a /source1/ /source2/ /path/to/targetdir/
You've no doubt already read the part of rsync's man page that explains the difference between source dirs with and without their trailing slash. If not, read up, because it's important.
If your "thousands of source files [with] similar names" need to be matched from within your source directories, leaving some other files behind, you need to determine whether your "similar names" can be differentiated using pathname expansion or if you should use a regular expression. If the former, then adding the pathname expansion to your sources with either mv or rsync should be sufficient. If you need to use a regex, then find may be a better option:
find /source1/ /source2/ -regex ".*/file[A-F][0-9][0-9].txt" -exec mv "{}" /targetdir/ \;
If these don't solve the problem, then you'll need to supply more detail in your question.
I would try a little shell script like this:
#!/bin/sh
cd /home/dir1
JPEGS=`find . -name "*.jpg"`
tar cf - $JPEGS | (cd /home/newdir1 ; tar xf -)
This first gets the list of all your jpg files with their relative paths, then writes a tar file of them to a pipe into a subshell which changes to the new directory, and then extracts the tar from its stdin.

Gitignore mac alias files

I have a fairly simple question I could not find a solution for. I want to ignore all Mac OS X Alias folders I have in my git project. How do I target those? I couldn't figure out their file extension. ".alias" and ".alias-file" doesn't seem to work.
Is there a way to do this? Other than ignoring each one specifically.
I don't know anything about git, but this script can find OSX aliases and list them for you, so you could probably pipe the list into git to ignore somehow...
#!/bin/bash
################################################################################
# ListAliases
# Given a directory as parameter, find and output names of all things under
# that directory that are aliases. Not an officially approved technique!
################################################################################
d=${1-$(pwd)}
find "$d" -exec sh -c 'xattr -pl com.apple.FinderInfo "{}" 2> /dev/null | grep -q alisMACS && echo "{}"' \;
Save it in a file called ListAliases, then make it executable like this:
chmod +x ListAliases
and run it like this
./ListAliases
or
./ListAliases /path/to/git/repository
Sample Output
/Users/mark/.Trash/installed.txt alias
/Users/mark/Desktop/installed.txt alias
/Users/mark/Desktop/installed.txt alias 2
It sounds like you're assuming that every file needs to have a file extension... that Mac alias files have a ".alias" extension. That's not the case. The Mac OS supports files both with and without extensions. When aliasing folders, there's no file extension involved. You would need to ignore the names of the folders that the aliases point to, which in turn would ignore the aliases themselves, since the aliases share the same name as the folders they point to.
What about aliases to folders?
With regard to folder alias' I tried this and it seemed to work:
*\ alias
In this case for all folder alias'
Just create a ".gitignore" file.
Write inside the patterns you want to ignore. (separated by new-line)
In your case that would be:
"
.alias*
"
Then, the "git status" command should not show you files OR directories matching the pattern.
Finish with a 'git add .gitignore' and "git commit -m "Added a gitignore file""
Update: Ok, the answer is very basic but I hadn't any clues.
Check this link : Ignore files that have already been committed to a Git repository

How to change the copy file behavior in mac osx

I used to use Ubuntu
most of time, the command are same as Ubuntu
But there is a command annoys me.
it is cp (copy command)
because I often type cp -a FOLDER/ target
to copy a file , however , the command will copy all the folder under FOLDER/ but not including FOLDER/ itself.
I know I can get what I want by using cp -a FOLDER target (without the slash in the tail)
it easily to misuse the command.
Is there any way to change the behavior, thanks
The command cp is a program in /bin so you could rename the cp command to another file, such as main_cp.exe and create a batch file (script) of your own called cp, which changes the call from what you type to what is actually required and then calls main_cp.exe.
However, if you're going to be using OSX a lot and perhaps on other people's machines, learning to train yourself to just not type the slash is probably going to be better in the long term.

Resources