Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 11 years ago.
Improve this question
My development machine is a linux host.
I have a complicated directory structure (like most of you, I assume), and I would like to move easily from one directory to the other, from within the shell. Specifically, welcomed features would be:
autocompletion (something like ido-mode in emacs)
regular expression directory / file matching
suggestion of recently visited directories (stack).
Possibilty to push/pop to the stack, get a listing of recently visited directories, ...
good integration of those features
console based
Do you know any tool which can satisfy those requirements?
In bash you can set CDPATH to a colon-separated directories that bash will search for when the argument to the cd does not exist.
$ man bash|grep -A3 '^\s\+CDPATH '
CDPATH The search path for the cd command. This is a colon-
separated list of directories in which the shell looks
for destination directories specified by the cd com‐
mand. A sample value is ".:~:/usr".
Once set, autocomplete will just work the way you'd expect it:
$ export CDPATH=dir1:dir2
$ cd somedir<tab>
Besides the current directory, bash will look into the directories in $CDPATH for the possible values.
Umm, any interactive shell(say, bash) already has nearly all of these features:
Press Tab once to auto-complete, and twice to show a list of possible completions.
find | grep reg.exp can be used for file matching, or find -exec grep reg.exp -H '{}' ';' to match contents
You can switch to the previous directory with cd -
pushd and popd can be used to push and pop directories
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
The explanation is:
"-R, --recursive
operate on files and directories recursively"
What does "recursive" mean here?
"Recursive" implies that the operation will be performed for all files and directories (and all files and directories within any directory). So
chown -R foo /some/path
would change file owner to foo for all files and directories in /some/path
p.s. You might have even seen the dictionary entry for recursive:
recursive, n: See recursive
In some Linux commands, if you run the command on a folder with -R, the command will operate on all files and folders in that folder's tree. If you run the command on a file, -R has no effect.
The command will operate on given folder, and recursively operates on files and folders within it. It is based on recursion.
For example, you can remove a folder and its contents with
rm -R folder-name
Or you can find all occurrences of a specific string in all files within current folder tree with
grep -R -n the-string .
In this example -n is for displaying line numbers.
It means apply it to sub-directories and their contents, that is, recurse chown() when a directory is encountered.
Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
Surprisingly I could not find a straight-forward answer to this question on here yet. I am still learning Linux. Say I have downloaded a zip file to my Downloads folder. Now, I want to move it into a protected folder, like /opts or /var. Is there a good command to both sudo move AND unzip the file to where I need it to go?
If you wish to perform two separate operations (move and extract) then you have no option but to use two commands.
However, if your end goal is to extract the zip file to a specific directory, you can leave the zip file where it is and specify an extraction directory using the -d option:
sudo unzip thefile.zip -d /opt/target_dir
From the manpage:
[-d exdir]
An optional directory to which to extract files. By default, all files and subdirectories are recreated in the current directory; the -d option allows extraction in an arbitrary directory (always assuming one has permission to write to the directory). This option need not appear at the end of the command line; it is also accepted before the zipfile specification (with the normal options), immediately after the zipfile specification, or between the file(s) and the -x option. The option and directory may be concatenated without any white space between them, but note that this may cause normal shell behavior to be suppressed. In particular, ''-d ~'' (tilde) is expanded by Unix C shells into the name of the user's home directory, but ''-d~'' is treated as a literal subdirectory ''~'' of the current directory.
sudo mv <file_name> /opts && unzip /opts/<file_name>
Also you may specify the unzip destination to unzip so you can do this in a single command. This however will be a bit different from the command above as the zip will be kept in its current location, only the unzipped files will be extracted to the pointed destination.
unzip -d [target directory] [filename].zip
Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
when I use * in cp, I think it follows the same rule as regex.
so "cp temp/* test/" should copies everything over, however, when temp folder is empty it throws exception saying it cannot find file or directory, which indicte * cannot match "nothing".
Then I create a file test.txt under temp and do:
cp temp/test.txt* test/
It works, which indicate * indeed match "nothing".
I get confused about the behavior. Can anyone explain a little bit?
Thanks
What's happening is the * expansion is done by your shell (bash probably). The pattern temp/testfile.txt* did match temp/testfile.txt (* matches zero or more characters), so bash passed that onto cp.
However, bash is set, by default, to pass the wildcard as-is on to the app if it doesn't match anything (there's an option called nullglob to turn this non-intuitive behavior off). So it passed temp/* literally to cp, which complained that it didn't exist.
The shell does the expansion, so it's not cp specific.
If not match is found, there's no substitution, the original string (temp/*) is reserved and passed to the application. Of course cp cannot find a file by that name.
# echo nosuchfile*
nosuchfile*
Some clarification for "nothing":
temp/* means entries (files/directories/...) in temp directory, but there weren't any files, so it failed.
temp/test.txt* means entries starting with test.txt in the temp directory.
Wildcard globbing is not the same as regular expressions, complete with their own rules.
Different shells have different rules ... you make want to look at Wikipdia to get an overview.
Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
I have a number of files such as file_022.bmp, file_023.bmp...file_0680.bmp. I need to rename these to something a little bit more convenient such as file_1.bmp, file_2.bmp...file_658.bmp.
Is there a bash script that I could write to do this for me? Thanks for the help and advice.
Luke H
if you're on a debian based linux system then you can use the rename script which accepts regular expressions to rename files. Some more info because I find it hard to find the man page.
e.g.
harald#Midians_Gate:~$ ls p*.php
parse.php pd.php pgrep.php preg_based.php proc.php
suppose I want to change the extension to .perl and prepend the name with file_
then I use command:
rename -n 's/([a-z]*)\.php/file_$1.perl/' p*.php
would give
parse.php renamed as file_parse.perl
pd.php renamed as file_pd.perl
pgrep.php renamed as file_pgrep.perl
preg_based.php renamed as preg_file_based.perl
proc.php renamed as file_proc.perl
I select and capture the base filename ([a-z]*) and then use it in the substitution $1 and append .perl and prepend $1 with the regular string file_
the -n option makes it test run without changing anything
As you can see from this example your selecting regexp needs to be correctly thought out or you get cases like the above preg_based.php where you wanted file_preg_based.perl :)
to compensate for that I would've needed to use ([a-z_]*) here
It's one of the many reasons why I keep hanging on to debian, I'd love to find the equivalent for other non-debian systems though :-/
if you have files a.bmp,b.bmp,c.bmp
and you want to end up with file_1.bmp, file_2.bmp, file_3.bmp
using bash:
mkdir result
index=1
for i in *.bmp
do
mv "$i" "result/file_"$((index++)).bmp
done
notes:
using a subdirectory is advised to avoid accidentally overwriting a file that looks like file_xx.bmp
if you have too many files to fit in the command line after expansion you could use something like:
mkdir result
index=1
find . -name "*.bmp" | while read i
do
echo mv "$i" "result/file_"$((index++)).bmp
done
after inspecting the output remove the 'echo'
Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 11 years ago.
Improve this question
I'm wondering if it's possible to list the content of a directory in linux after erasing it and recreating.
Explanation: I'm on a terminal in this particular directory. From another terminal, I erase it and recreate it and put some content inside. If I list this directory from the first terminal, it appears as empty. I need to cd .. and enter inside again to list it's content.
Is there another method who doesn't need to do that?
The only way, as far as I know, is to cd in it again. However, you can do it with one simple command. Select which one you like the most between the following four
cd ${PWD}
cd $PWD
cd $(pwd)
cd `pwd`
You can also add to your ~/.bashrc an alias like this:
alias refresh_dir="cd \$PWD"
and then call the refresh_dir command directly
Would ls ../{directory-name} work? So if the directory was called "test", and you were inside it, you'd use the command ls ../test/.
Nope. You can, of course,
cd "$PWD"
which is a quicker way in a sense.
Some shells might not cache the inode for the current working directory, but I have a sneaking suspicion that POSIX might require shells to. If you think about it, having the shell do this automatically might result in unintended loss of data (because a program/script might go and modify stuff in a directory that wasn't strictly it's working directory to begin with).
Also look at bash PROMPT_COMMAND for a hint on how to automate it, of you find yourself having to type cd "$PWD" more than you like
terminal 1:
mkdir dir/
cd dir/
touch foo/
ls
terminal 2:
rm -r dir/
mkdir dir/
touch dir/bar
terminal 1:
cd `pwd`
ls