0

If I run

$ git status | grep modified:

I got 

modified: config/express.js                                                                      
modified: package.json                                       
modified: routes/index.js

What do I need to run to get just the name ( NOT the entire path) of the files that I modified ?

Ex.

express.js                                                                      
package.json                                       
index.js

This is NOT a dupe of is it possible to git status only modified files?

I am looking for the file name only ---- NOT the file path + file name .

code-8
  • 54,650
  • 106
  • 352
  • 604
  • @iab. The answer gives me the entire path. I am looking for the *name* only. – code-8 Nov 09 '17 at 23:20
  • 1
    What do you want to do if the output suggests that both `a/foo.txt` and `b/foo.txt` are modified? Claim that `foo.txt` is modified twice? In any case you'll need to post-process the output from some command, to strip it down to just base names; you may want to sort and remove duplicates at that point as well. – torek Nov 09 '17 at 23:21
  • @ExplosionPills : The answer gives me the entire path. I am looking for the name only. – code-8 Nov 09 '17 at 23:21
  • @torek : You're right and got your point. I will need to make sure consider the case with the same file name on a diff route, but the goal of this post mean to be simple, and get the filename of `modified` file(s) -- that's it. I appreciate your concern. – code-8 Nov 09 '17 at 23:25
  • What are you trying to do specifically? – Explosion Pills Nov 09 '17 at 23:35
  • @ExplosionPills : **Scope** is already answered what I am looking for. – code-8 Nov 09 '17 at 23:36
  • 1
    Sure, but what is your ultimate goal in doing this? Like what do you want to do with this list of files? – Explosion Pills Nov 09 '17 at 23:37

4 Answers4

4

This should work:

git diff --name-only --diff-filter=M | xargs -L 1 basename

Edited to include line-by-line handling in xargs command.

SEoF
  • 1,092
  • 14
  • 26
scope
  • 1,967
  • 14
  • 15
  • Now, your is the first one that works. Finally someone get me. – code-8 Nov 09 '17 at 23:27
  • I added --diff-filter to make it fully working. – scope Nov 09 '17 at 23:29
  • The first one is already working. What diff in the background if you don't mind enlighten us a bit on this .. – code-8 Nov 09 '17 at 23:35
  • First one was showing also `added` and `deleted` files, this one shows only `modified`. – scope Nov 09 '17 at 23:37
  • Got it !! precisely. – code-8 Nov 09 '17 at 23:38
  • Unfortunately, `basename` does not accept an arbitrary number of file names on its command line. You have to do ... `| xargs -n1 basename` or some such to have it work correctly for the situation where the pipeline contains more than one file name. (There is another answer which does this correctly, which should perhaps be accepted instead.) – tripleee Nov 10 '17 at 05:13
2
git ls-files -m | xargs -L 1 basename

You need the -L 1 arguments to xargs to make a new call to basename for each file. Otherwise the command only ever lists a single result

briggySmalls
  • 71
  • 2
  • 5
0

you could run

git status | awk '{print $2}'

or

without the full filename:

git status | grep modified | awk -F "/| " '{print $NF}'

May want to pipe grep in the middle

spitfire21
  • 103
  • 4
-1

You can user awk like this:

git status | grep modified | awk '{split($2, a, "/"); print a[1]}'

EDIT: changed : to ;

ingroxd
  • 995
  • 1
  • 12
  • 28
  • Awk does almost everything `grep` does, so almost every script which looks like `thing | grep A | awk '{ B }'` can be improved to `thing | awk '/A/ { B }'`. See also [uesless use of `grep`](http://www.iki.fi/era/unix/award.html#grep). – tripleee Nov 10 '17 at 05:11
  • 1
    The Awk part presupposes that file names contain exactly zero or one slashes, and as far as I can tell prints the top-level directory, not the file name, for the ones which have one or more. A script to extract just the final part would be `{ n=split($2, a, "/"); print a[n] }` – tripleee Nov 10 '17 at 05:15
  • @tripleee The output to parse contains only a '/' (E.G. `modified: config/express.js`) – ingroxd Nov 10 '17 at 10:14
  • In the general case, there is no guarantee that this is the case. Your script still extracts `config` not `express.js`. – tripleee Nov 10 '17 at 10:16
  • Oh and you have a colon instead of a semicolon before the `print`. – tripleee Nov 10 '17 at 10:17
  • Corrected colon, thanks [: And yes, n have to be stored to show the correct var! – ingroxd Nov 10 '17 at 10:25