7
votes
6answers
602 views

Is it possible to remove folder prefix from a `ls` command

I am in a bash script and I want to get the list of all files (let say all jar files). I execute the command ls -1 lib/*.jar and I get the output: lib/mylib_1.jar lib/mylib_2.jar ... Is there any ...
2
votes
2answers
20 views

Symlink all subdirectories in a directory to another directory

I'm trying to symlink every directory in a folder to another folder. For example, in the structure below, I need the subfolders symlinked to folder2. - /home/chris/folder1 -- subfolder1 -- subfolder2 ...
3
votes
3answers
218 views

Transform a directory into file or a file into directory

I wanted to create a file named test. Accidentally I run mkdir test instead of touch test. Is it possible to convert test directory in a file named test? What about converting a file named test into ...
4
votes
2answers
353 views

Where is a shell script run from?

I have a shell script in /usr/bin. Let's call it hello. It contains this: echo "I am located in $PWD" echo "I am called from this directory: ???" Running hello from /home/Documents I expect the ...
-2
votes
2answers
29 views

How do I list lhe file permissions of only the current directory? [duplicate]

ls -la . will show this in it: insgesamt 1312 drwxrwxr-x 6 ruben rubo77 4096 Feb 23 06:20 . drwxrwxr-x 23 ruben rubo77 4096 Feb 6 21:48 .. ... But it will show the whole content of the ...
4
votes
1answer
40 views

ls -l --group-directories-first (act on symlinks as well)

ls option --group-directories-first causes directories to be listed on the top, which makes the output of ls nice and clean: ls -l --group-directories-first However, it does not act on symlinks, ...
3
votes
3answers
112 views

find with execdir

I run a find with execdir both I don't get the results I was expecting. SSCCE below: mkdir -p a/b/c find . -type d -execdir touch foo \; $ tree a a ├── b │   ├── c │   └── foo └── foo Directory c ...
2
votes
1answer
83 views

Check for existence of files prior to acting on them

In one of my processes I check for existence of files older than 31 days, and then do stuff with them. find /my/directory/*txt -mtime +31 -exec ls -l {} \; All this is well and fine if files are ...
1
vote
2answers
111 views

How to find certain files and move them to a new directory in the same command?

So I was looking around and all I have found is how to do this only if the directory is already made, which is this: find source -name '*.z' -exec cp {} destination \; But how can I make a new ...
4
votes
2answers
315 views

Recursively add a file to all sub-directories

How do I recursively add(or touch) a file into the current directory, as well as all sub-directories? For example, I would like to turn this directory tree: . ├── 1 │   ├── A │   └── B ├── 2 │   └── ...
0
votes
1answer
62 views

Recursively Move All MP3's in a Directory [duplicate]

I have a lot of mp3 files located at various levels of subdirectories inside a single folder and I want to move them all to a single directory, what command could I use to do this? Thanks!
20
votes
9answers
1k views

What's the most resource efficient way to count how many files are in a directory?

CentOS 5.9 I came across an issue the other day where a directory had a lot of files. To count it, I ran ls -l /foo/foo2/ | wc -l Turns out that there were over 1 million files in a single ...
2
votes
1answer
71 views

bash - inherit directory stack in children

If, in my newly created bash, I do: pushd foo pushd bar dirs -l -v I will get something like 0 bar 1 foo 2 old_dir If I then do bash dirs -l -v I get only 0 bar The same thing happens ...
3
votes
2answers
428 views

Automatically moving files to a directory, one by one, and only when the target folder is empty

Is it possible? And in reverse alphabetical order? Essentially, this: How can I move files by type recursively from a directory and its sub-directories to another directory? Except that each file is ...
20
votes
8answers
4k views

How do I loop through only directories in bash?

I have a folder with some directories and some files (some are hidden, beginning with dot). for d in *; do echo $d done will loop through all files, but I want to loop only through directories. ...
0
votes
1answer
206 views

Find and remove sub-directories without error?

I need to find a way to remove sub-directories from within a particular directory without receiving an error if no sub-directories exist. This command will successfully remove sub-directories if they ...
3
votes
3answers
165 views

Quick access to work directory of background job

Minimal effort to reproduce what I am looking for, is as follows /$ sleep 1h & [1] 6564 /$ cd ~$ jobs [1]+ Running sleep 1h & (wd: /) When I use jobs to manage my ...
3
votes
2answers
70 views

Refer to an item in `dirs`

I use pushd to work with multiple directories in bash and zsh. I've aliased dirs to dirs -v so that I get an ordered list when I want to see what's on the directory stack: chb$ dirs 0 ...
5
votes
3answers
455 views

Walking through files in directory randomly

How to change for track in *.mp3; do so that all .mp3 files in the current directory are walked through but in a random way?
3
votes
2answers
1k views

find flags: -exec rm -rf vs -delete

I thought the flags I mentioned in the question are the same, but I get the following message with the former, but nothing with the latter: $ find . -mindepth 1 -type d -exec rm -rf {} \; find: ...
7
votes
7answers
3k views

How do I create a directory in all subdirectories?

Suppose I have a directory structure like this: $ [~/practice] ls a/ b/ c/ d/ Now I want to create a directory tmp1 in all sub directories of practice and I do this: $ [~/practice] mkdir */tmp1 ...
0
votes
2answers
161 views

/: is a directory issue in bash

I'm quite new to bash scripting and have been going reasonably well but have run into an issue. I am trying to create a script that uses a forward slash (/), however, I keep getting the error message ...
5
votes
3answers
736 views

How to set an alias on a per-directory basis?

Suppose you have an alias go, but want it to do different things in different directories? In one directory it should run cmd1, but in another directory it should run cmd2 By the way, I have an ...
2
votes
2answers
576 views

Using wildcards to match a directory in bash

Lets say the folder structure is like so: /home/ --user1/asdf --user2/asdf1234 --user3/asdf325234 --cool/asdf How could I change to asdf1234 without specifying the user? For example: cd ...
1
vote
3answers
109 views

Restructuring directories into a hierarchy

I have the following type of folder structure containing thousands of folders. The folder names are as such with different names etc .test .test.subfolder .test.subfolder.subsubfolder .folder ...
4
votes
2answers
757 views

Why can't I navigate into my Documents directory?

I'm in bash and running F17 as su. My pwd is /home/user and when I ls I see Desktop Documents Downloads Music Pictures Public Templates Videos However, I'm unable to cd into any of these. My ...
0
votes
2answers
1k views

How can I rename all files in the current directory having a particular extension to another extension? [duplicate]

Possible Duplicate: Renaming multiple files (changing extension) Suppose that in my current/working directory I have five files: file1.xvg file2.xvg file3.xvg file1.eps file2.eps Is ...
3
votes
3answers
9k views

How can I delete all files with a particular extension in a particular folder?

If I set the current/working directory (navigating to it using cd) to some particular directory and then type: rm *.xvg What will this command do? Is it true that the above command will only delete ...
1
vote
1answer
158 views

How to find/correct error in a path

It's often that a long path is mistyped or you get a log file entry that a path does not exist. Is there a command or shell function that navigates the path hierarchy until it finds a matching path? ...
2
votes
4answers
205 views

Say I have a file's path, how do I reference that file's directory from the command line?

I can type: dirname ~/home/blah/file.zip for instance, and this works fine alone, but when I use this syntax as the parameter for some command it always reads 'dirname' as the desired directory. ...
4
votes
2answers
2k views

What causes ln: //: Is a directory?

When linking a directory to root, I get this error: $ ln -s ~/inbox/ / $ ln: //: Is a directory Bash autocompletes the directory path by adding a /. I've tried escaping without success. $ ln -s ...
7
votes
4answers
4k views

How can I force unzip / zip not to create a subdirectory when I extract it?

Depending on how a zip file is created, sometimes it will extract all of the files directly, and sometimes it will extract the files into a subdirectory. If the latter is true, how can I force the ...
1
vote
2answers
534 views

Bash prompt when my home directory is a symbolic link

I symlinked my /host/Users/Kevin folder to /home/Kevin. I also edit the /etc/passwd file to /home/Kevin. I logged out and logged back in, however when I open up a terminal it's a bash prompt like so: ...
35
votes
3answers
1k views

cd by just typing the directory's name?

I either had this somewhere 20 years ago or I dreamed about it. Basically: If if type blobblob I get blobblob: command not found Fair enough. I would like it so that when my shell gets those ...
9
votes
4answers
3k views

symbolic link to a directory and relative path

I've created symlink with absolute path to the directory (Blink) and have for example following tree: $ ls -l /tmp/A total 0 lrwxrwxrwx 1 root root 6 Apr 3 12:27 Blink -> /tmp/B -rw-r--r-- 1 root ...
-1
votes
2answers
490 views

Why can't I create a directory by full relative path argument?

Why can't I create a directory by full relative path argument? Here is what I try: #!/bin/bash args=("$@") # Print the command and run it. Exit the script on failure. run() { if $VERBOSE; ...
5
votes
3answers
2k views

Find files with same name but different content?

I want to generate a list of files that have: Same name Different content in a directory (including all children directories and content). How to do? Bash, perl, anything is fine. So, two ...
0
votes
2answers
1k views

How to calculate in linux number of file inside nested directories?

I need to extract information how much files are in some directory tree: /.../testRoot/test1/test11/..../file1 /.../testRoot/test1/test11/file2 /.../testRoot/test1/test11/..../file3 ...
11
votes
4answers
744 views

How do I choose specific files in a different directory using bash?

I want to list (or delete, or do some other operation) on certain files in a directory, like this: $ ls /opt/somedir/ aa bb cc aa.txt bb.txt cc.txt $ ls /opt/somedir/(aa|bb|cc) ## pseudo-bash ...
7
votes
2answers
698 views

Is there a correct way to list the subdirectories of the current directory?

I can find the subdirectories of a directory by ls -d -- */ or ls -l | grep "^d" but both of these seem indirect, and I would imagine that there would be a standard way to find just the ...
8
votes
5answers
1k views

Bash CD up until in certain folder

I do a lot of work in Magento, and so do a lot of the people I work with, and it gets annoying to have to type: cd ../../../../../../ To only find you're still a few directories from your root ...
2
votes
1answer
230 views

Switch back to path when resume job from background

Say I open ~/vim.txt, push that to background, then cd to another path. When I bring that job to foreground, is there an option to switch back to old path? I noticed it says pwd, so I assume it is ...
4
votes
2answers
450 views

Shell script printing the most visited directories

Is there a way to write a bash script with the following functionalities? Be launched when I press some key or key combination. (not so important requirement) Identify the 7 most visited directories ...
2
votes
3answers
5k views

use 'find' to search for directories !containing certain filetype foo

I have a few directories, some with a depth of 3, which contain mixed file types. What I need to do is to rm -rf all the subdirectories that do not contain filetype foo. Is this achievable with find ...
6
votes
7answers
4k views

How to check if $PWD is a subdirectory of a given path

E.g. check if $PWD is a subdirectory of /home. In other words I'm searching for a bash string operation to check if one string starts with another.